-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflask_proxy.py
More file actions
34 lines (28 loc) · 992 Bytes
/
flask_proxy.py
File metadata and controls
34 lines (28 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
"""http proxy server by flask and requests"""
from contextlib import closing
import requests
from flask import Flask, request, Response
app = Flask(__name__)
@app.before_request
def before_request():
url = request.url
method = request.method
data = request.data or request.form or None
headers = dict()
for name, value in request.headers:
if not value or name == 'Cache-Control':
continue
headers[name] = value
with closing(
requests.request(method, url, headers=headers, data=data, stream=True)
) as r:
resp_headers = []
for name, value in r.headers.items():
if name.lower() in ('content-length', 'connection',
'content-encoding'):
continue
resp_headers.append((name, value))
return Response(r, status=r.status_code, headers=resp_headers)
if __name__ == '__main__':
app.run(port=8888, debug=True)