Reputation: 23
from flask import Flask,request,redirect,Response
import requests
app = Flask(__name__)
SITE_NAME = 'https://www.whatsmyua.info'
@app.route('/',methods=['GET'],)
def proxy():
#print(path)
global SITE_NAME
#print(f'Current path: {path}')
headers = {"User-Agent": f"{request.user_agent}"}
if request.method=='GET':
resp = requests.get(f'{SITE_NAME}', headers=headers)
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)
print(request.user_agent)
print(request.path)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
When I request http://127.0.0.1/test, it acts as a reverse proxy for https://www.whatsmyua.info/. The page loads, but the css and js needed by the page don't. How do I fix this?
127.0.0.1 - - [16/Sep/2021 13:21:37] "GET /test HTTP/1.1" 200 - #only index loads
127.0.0.1 - - [16/Sep/2021 13:21:37] "GET /css/style.css HTTP/1.1" 404 - #these 3 don't load because they're in sub-directories.
127.0.0.1 - - [16/Sep/2021 13:21:37] "GET /js/app-built.js HTTP/1.1" 404 -
127.0.0.1 - - [16/Sep/2021 13:21:37] "GET /js/app-built.js HTTP/1.1" 404 -
Upvotes: 0
Views: 687
Reputation: 886
from flask import Flask,request,redirect,Response
import requests
app = Flask(__name__)
SITE_NAME = 'https://www.whatsmyua.info'
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
global SITE_NAME
headers = {"User-Agent": f"{request.user_agent}"}
if request.method=='GET':
resp = requests.get(f'{SITE_NAME}/{path}', headers=headers)
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)
print(request.user_agent)
print(request.path)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Create a route to catch all the requests and make requests request URL f'{SITE_NAME}/{path}'. But I suggest you to use something more suitable for reverse proxying like nginx.
Upvotes: 1