Spaces:
Paused
Paused
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, Response
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
PROXY_TARGET = "https://gem-bingo.hf.space"
|
| 7 |
+
|
| 8 |
+
@app.route('/')
|
| 9 |
+
def home():
|
| 10 |
+
global PROXY_TARGET
|
| 11 |
+
# Get the parameters of the original HTTP GET request
|
| 12 |
+
resp = requests.get(f"{PROXY_TARGET}{request.full_path}")
|
| 13 |
+
# Returns response data obtained from the target website
|
| 14 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 15 |
+
headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
| 16 |
+
if name.lower() not in excluded_headers]
|
| 17 |
+
response = Response(resp.content, resp.status_code, headers)
|
| 18 |
+
return response
|
| 19 |
+
|
| 20 |
+
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
| 21 |
+
def proxy(path):
|
| 22 |
+
global PROXY_TARGET
|
| 23 |
+
# Construct target URL
|
| 24 |
+
url = f"{PROXY_TARGET}/{path}"
|
| 25 |
+
# Send the corresponding request to the target server according to the original request method
|
| 26 |
+
if request.method == 'GET':
|
| 27 |
+
resp = requests.get(url, stream=True)
|
| 28 |
+
elif request.method == 'POST':
|
| 29 |
+
resp = requests.post(url, json=request.json, stream=True)
|
| 30 |
+
elif request.method == 'PUT':
|
| 31 |
+
resp = requests.put(url, json=request.json, stream=True)
|
| 32 |
+
elif request.method == 'DELETE':
|
| 33 |
+
resp = requests.delete(url, stream=True)
|
| 34 |
+
|
| 35 |
+
# Return the response obtained from the target website to the client
|
| 36 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 37 |
+
headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
| 38 |
+
if name.lower() not in excluded_headers]
|
| 39 |
+
|
| 40 |
+
def generate():
|
| 41 |
+
for chunk in resp.iter_content(chunk_size=8192):
|
| 42 |
+
yield chunk
|
| 43 |
+
|
| 44 |
+
return Response(generate(), resp.status_code, headers)
|
| 45 |
+
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|