简体   繁体   English

带请求模块的Python Flask

[英]Python Flask With Requests Module

I am using a Python server to make a http call with Python's Requests module. 我正在使用Python服务器使用Python的请求模块进行http调用。 The requests call works with no issues in a regular python shell. 请求调用在常规python shell中没有问题。 But in Flask, it errors out with "raise ConnectionError(e)". 但是在Flask中,它会因“ 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. 这确实很痛苦,因为由于跨域限制,我不能使用javascript,因此我需要使用python在后端进行url调用。

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. 它在您的云服务器上不起作用的原因是10. * IP范围是私有的,不能公开路由。

It is the same as being on 192.168.* and 172. etc. 与192.168。*和172上的相同。

You need to bind the 10.44.11.33 to a different IP address. 您需要将10.44.11.33绑定到其他IP地址。 One which is accessible over the internet. 可以通过互联网访问的一种。

Joe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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