~nhanb/gae-proxy

ref: 4f225109bb699eeebf3636a18c86fcfa8b9d30c6 gae-proxy/main.py -rw-r--r-- 1.0 KiB
4f225109Bùi Thành Nhân auth 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os

import requests
from bottle import HTTPResponse, request, route, run

# GAE recommended
PORT = os.environ.get("PORT", 8080)
PASSWORD = os.environ.get("GAEPROXY_PASSWORD", "")

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


@route("/proxy", method="POST")
def proxy():
    req = request.json

    if req["method"] not in proxiable_methods:
        return HTTPResponse(status=400, body="We serve get/post only!")

    if req["password"] != PASSWORD:
        return HTTPResponse(status=400, body="Get off my lawn!")

    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=PORT)