Flask-Proxy makes it easier to create proxy pass router in Flask.
- From PyPI:
pip install Flask-Proxy
- Clone:
git clone [email protected]:mecforlove/flask-proxy.git
cd flask_proxy
python setup.py install
There is a simple example to make a proxy pass for httpbin.org.
First, create a python file named httpbin.py
:
from flask import Flask
from flask_proxy import Proxy, Upstream
class Httpbin(Upstream):
prefix = '/httpbin'
host = 'httpbin.org'
routes = [{
'url': '/get',
'methods': ['GET'],
}, {
'url': '/post',
'methods': ['POST'],
}]
app = Flask(__name__)
proxy = Proxy(app)
proxy.add_upstream(Httpbin)
app.run()
Then we just run the flask app above:
$ python httpbin.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
At last, we open another terminal and test it with curl:
$ curl http://localhost:5000/httpbin/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.54.0"
},
"origin": "47.90.41.239",
"url": "http://httpbin.org/get"
}
Everything is done!