本地主机IP(127.0.0.1)在google-compute-engine上不起作用

时间:2019-09-02 11:13:33

标签: python python-requests localhost google-compute-engine

我公开了一个URL(http://127.0.0.1:5000/daily),但是在Google Compute Engine(GCE)中,我没有得到这些值。如果我通过简单的python程序中的请求访问此URL,则该URL有效运行。

import requests
import json
req=requests.get('http://127.0.0.1:5000/daily')

a = json.loads(req.text)

discount_rate = a['data']['policy_rate']
six_months_kibor = a['data']['today_kibor_rate']
dollar_to_pkr= a['data']['today_usd_rate']

print(discount_rate, six_months_kibor, dollar_to_pkr)
我从GCE收到的

错误是:

urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f93526c16a0>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /daily (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f93526c16a0>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    req=requests.get('http://127.0.0.1:5000/daily')
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/home/dev_baseh/.local/lib/python3.5/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /daily (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f93526c16a0>: Failed to establish a new connection: [Errno 111] Connection refused', )) 

我不知道为什么它没有运行在GCE之上。 在此先感谢:)

1 个答案:

答案 0 :(得分:2)

IP地址127.0.0.1是指计算机的本地IP地址。因此,如果您在运行该服务器的同一台计算机上运行python程序,则该服务器将能够访问该地址,因为两者具有相同的IP地址。

当您尝试从GCP访问127.0.0.1时,发生的情况是GCP在本地尝试访问端口5000,而不是计算机的端口5000。

您需要确定运行服务器的计算机的面向公众的IP地址。如果在您的计算机上,则只需使用Google“我的IP是什么”即可获取。