Spaces:
Paused
Paused
| from flask import Flask, request, Response | |
| import requests | |
| app = Flask(__name__) | |
| PROXY_TARGET = "https://gem-bingo.hf.space" | |
| def home(): | |
| global PROXY_TARGET | |
| # Get the parameters of the original HTTP GET request | |
| resp = requests.get(f"{PROXY_TARGET}{request.full_path}") | |
| # Returns response data obtained from the target website | |
| excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] | |
| headers = [(name, value) for (name, value) in resp.raw.headers.items() | |
| if name.lower() not in excluded_headers] | |
| response = Response(resp.content, resp.status_code, headers) | |
| return response | |
| def proxy(path): | |
| global PROXY_TARGET | |
| # Construct target URL | |
| url = f"{PROXY_TARGET}/{path}" | |
| # Send the corresponding request to the target server according to the original request method | |
| if request.method == 'GET': | |
| resp = requests.get(url, stream=True) | |
| elif request.method == 'POST': | |
| resp = requests.post(url, json=request.json, stream=True) | |
| elif request.method == 'PUT': | |
| resp = requests.put(url, json=request.json, stream=True) | |
| elif request.method == 'DELETE': | |
| resp = requests.delete(url, stream=True) | |
| # Return the response obtained from the target website to the client | |
| excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] | |
| headers = [(name, value) for (name, value) in resp.raw.headers.items() | |
| if name.lower() not in excluded_headers] | |
| def generate(): | |
| for chunk in resp.iter_content(chunk_size=8192): | |
| yield chunk | |
| return Response(generate(), resp.status_code, headers) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) |