Skip to content

Instantly share code, notes, and snippets.

@matabares
Created July 20, 2020 19:41
Show Gist options
  • Save matabares/592e815a96d39921789aefe959cffa8d to your computer and use it in GitHub Desktop.
Save matabares/592e815a96d39921789aefe959cffa8d to your computer and use it in GitHub Desktop.
Reverse Proxy in flask proof of concept
from flask import Flask, request, redirect, Response
import requests
app = Flask(__name__)
@app.route('/')
@app.route('/<path:path>', methods=['GET', 'POST', 'DELETE'])
def proxy(path=""):
SITE_NAME = 'https://www.google.com/'
print(request.url)
if request.method == 'GET':
resp = requests.get(f'{SITE_NAME}{path}')
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
elif request.method == 'POST':
resp = requests.post(f'{SITE_NAME}{path}', json=request.get_json())
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
elif request.method == 'DELETE':
resp = requests.delete(f'{SITE_NAME}{path}').content
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
if __name__ == '__main__':
app.run(debug=False, port=8009)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment