~nhanb/gae-proxy

5b559a26a393046b487f39f3f4874e7b557c83be — Bùi Thành Nhân 2 years ago
init
4 files changed, 52 insertions(+), 0 deletions(-)

A app.yaml
A main.py
A requirements.txt
A send.py
A  => app.yaml +2 -0
@@ 1,2 @@
runtime: python37
secure: always

A  => main.py +27 -0
@@ 1,27 @@
import requests
from bottle import HTTPResponse, request, route, run

proxiable_methods = {
    "get": requests.get,
    "post": requests.post,
}


@route("/proxy", method="POST")
def proxy():
    req = request.json
    assert req["method"] in proxiable_methods
    requests_func = proxiable_methods[req["method"]]
    requests_kwargs = {"url": req["url"], "headers": req["headers"]}
    if req["method"] == "post":
        requests_kwargs["body"] = req["body"]

    try:
        resp = requests_func(**requests_kwargs, timeout=10)
    except Exception as e:
        return HTTPResponse(status=500, body=f"Unexpected error:\n{e}")

    return HTTPResponse(body=resp.text, status=resp.status_code)


run(host="0.0.0.0", port=8080)

A  => requirements.txt +2 -0
@@ 1,2 @@
requests
bottle

A  => send.py +21 -0
@@ 1,21 @@
import requests

"""
Lazy quick test script
"""

resp = requests.post(
    "http://localhost:8080/proxy",
    json={
        "url": "https://httpbin.org/get",
        "method": "get",
        "body": None,
        "headers": {"Foo-Bar": "ehhhh"},
    },
)

print(resp.status_code)
for hkey, hval in resp.headers.items():
    print(hkey, ":", hval)

print(resp.text)