cloudscraper / proxy.py
edwagbb's picture
Update proxy.py
9db2cbd
raw
history blame
1.18 kB
from flask import Flask, request, Response
import requests
import cloudscraper
scraper = cloudscraper.create_scraper(interpreter='nodejs')
app = Flask(__name__)
app.url_map.strict_slashes = False
@app.route('/proxy/<scheme>://<path:url>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(scheme, url):
req_method = request.method
req_headers = {key: value for key, value in request.headers if key != 'Host'}
req_data = request.get_data()
resp = scraper.request(method=req_method,
url=f'{scheme}://{url}',
headers=req_headers,
data=req_data,
stream=True)
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=1):
yield chunk
return Response(generate(), status=resp.status_code, headers=headers)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)