proxy error when using flask as a simple http proxy
<p>There my code:</p>

<p>main.py:</p>

<pre><code>from flask import Flask, request, Response
import requests

app = Flask(__name__)

@app.before_request
def before_request():
    url = request.url
    method = request.method
    data = request.get_data()
    headers = dict()
    for name, value in request.headers:
        if not value or name == 'Cache-Control':
            continue
        headers[name] = value

    r = requests.request(method, url, headers=headers, data=data, stream=True)
    response_headers = []
    for name, value in r.headers.items():
        if name.lower() in ('content-length', 'connection', 'content-encoding'):
            continue
            response_headers.append((name, value))
    return Response(r, status=r.status_code, headers=response_headers)


if __name__ == '__main__':
    app.run()
</code></pre>

<p>run.py:</p>

<pre><code>from main import app
app.run(host='127.0.0.1', port=6666, debug=True)
</code></pre>

<p>I run in console like :</p>

<pre><code>python run.py
</code></pre>

<p>It works fine for one request(finish one request and then next request).But it is stupid for it serves one request at a time.so I run is on gunicorn :</p>

<pre><code>gunicorn -c gun.conf main:app
</code></pre>

<p>I make 10 workers to run this app.But it fails.</p>

<pre><code>--File "/Users/xiazhibin/.pyenv/versions/2.7.11/envs/flask_study/lib/python2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
--File "/Users/xiazhibin/.pyenv/versions/2.7.11/envs/flask_study/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
--File "/Users/xiazhibin/.pyenv/versions/2.7.11/envs/flask_study/lib/python2.7/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
--File "/Users/xiazhibin/.pyenv/versions/2.7.11/envs/flask_study/lib/python2.7/site-packages/requests/adapters.py", line 485, in send
raise ProxyError(e, request=request)
ProxyError: HTTPConnectionPool(host='127.0.0.1', port=6666): Max retries exceeded with url:http://www.some_web_site.com/ 
(Caused by ProxyError('Cannot connect to proxy.',     NewConnectionError('&lt;requests.packages.urllib3.connection.HTTPConnection object at 0x108363bd0&gt;: 
Failed to establish a new connection: [Errno 61] Connection refused',)))
</code></pre>

<p>I don't know why this is the case.</p>

<p>Yes, it fails to connect to the proxy(127.0.0.1:6666)</p>

python flask python-requests