Created
April 25, 2023 07:20
-
-
Save typhoonzero/9ed92b7cb89c5a3d8165754c67ca584a to your computer and use it in GitHub Desktop.
Starting a web server mirroring a specific website. Set the value of target_site to use.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, make_response | |
import requests | |
app = Flask(__name__, static_folder=None) | |
target_site = "https://cn.bing.com/" | |
general_headers = { | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" | |
} | |
def proxy_request(request_path): | |
if request_path.startswith("/"): | |
request_path = request_path[1:] | |
target_url = target_site + request_path | |
if request.method == "POST": | |
# FIXME: only json supported | |
posted_data = request.json | |
tresp = requests.post(target_url, json=request.json, headers=general_headers, cookies=request.cookies) | |
else: | |
tresp = requests.get(target_url, params=request.args, headers=general_headers, cookies=request.cookies) | |
if tresp.status_code != 200: | |
print("error response: ", target_url, tresp.content, tresp.status_code) | |
resp = make_response(tresp.content, tresp.status_code) | |
resp.headers["Content-Type"] = tresp.headers["Content-Type"] | |
for k, v in tresp.cookies.items(): | |
resp.set_cookie(k, v) | |
return resp | |
@app.route("/", methods=['GET']) | |
def proxied_base(): | |
return proxy_request("/") | |
@app.route("/<path:request_path>", methods=['GET', 'POST']) | |
def proxied_site(request_path): | |
return proxy_request(request_path) | |
@app.route("/static/<path:request_path>", methods=['GET', 'POST']) | |
def proxied_static(request_path): | |
path = "/static/" + request_path | |
return proxy_request(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment