简体   繁体   中英

Python Flask With Requests Module

I am using a Python server to make a http call with Python's Requests module. The requests call works with no issues in a regular python shell. But in Flask, it errors out with "raise ConnectionError(e)". This really hurts because I need to make the url call on the back end with python since I can't with javascript because of cross-origin restrictions.

flask python # cat myapp.py
import json
import requests
from flask import Flask
app = Flask(__name__)


@app.route('/', defaults={'path': ''}, methods=('GET', 'POST'))
@app.route('/<path:path>', methods=('GET', 'POST'))
def catch_all(path):
    link = "http://10.44.11.33/api/v1/counts/latest/15"
    r = requests.get(link)
    return "Hello World"


if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0")

Error output:

File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
Display the sourcecode for this frameOpen an interactive python shell in this framereraise(exc_type, exc_value, tb)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/python/myapp.py", line 13, in catch_all
r = requests.get(core_link)
File "/usr/lib64/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/lib64/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib64/python2.7/site-packages/requests/sessions.py", line 335, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib64/python2.7/site-packages/requests/sessions.py", line 438, in send
r = adapter.send(request, **kwargs)
File "/usr/lib64/python2.7/site-packages/requests/adapters.py", line 327, in send
raise ConnectionError(e)

The reason it does not work from your cloud server is the 10.* IP range is private and not publically routable.

It is the same as being on 192.168.* and 172. etc.

You need to bind the 10.44.11.33 to a different IP address. One which is accessible over the internet.

Joe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM