Python 建立新连接失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39064126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:50:36  来源:igfitidea点击:

Failed to establish a new connection

pythonpython-2.7ubuntu-14.04

提问by mohamed desouky

import requests

url = 'http://ES_search_demo.com/document/record/_search?pretty=true'
data = '{"query":{"bool":{"must":[{"text":{"record.document":"SOME_JOURNAL"}},{"text":{"record.articleTitle":"farmers"}}],"must_not":[],"should":[]}},"from":0,"size":50,"sort":[],"facets":{}}'
response = requests.get(url, data=data)

when i run this code i get this error

当我运行此代码时,我收到此错误

    Traceback (most recent call last):
  File "simpleclient.py", line 6, in <module>
    response = requests.get(url, data=data)
  File "/home/ryan/local/lib/python2.7/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/home/ryan/local/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/ryan/local/lib/python2.7/site-packages/requests/sessions.py", line 471, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ryan/local/lib/python2.7/site-packages/requests/sessions.py", line 581, in send
    r = adapter.send(request, **kwargs)
  File "/home/ryan/local/lib/python2.7/site-packages/requests/adapters.py", line 481, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='es_search_demo.com', port=80): Max retries exceeded with url: /document/record/_search?pretty=true (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f544af9a5d0>: Failed to establish a new connection: [Errno -2] Name or service not known',))

回答by miraculixx

NewConnectionError(': Failed to establish a new connection: [Errno -5] No address associated with hostname',))

NewConnectionError(': 建立新连接失败: [Errno -5] 没有与主机名关联的地址',))

In other words, your DNS does not have an IP address for the given host. Since there is, however, a valid IP address with the domain name given originally, probably there is no connection to the internet:

换句话说,您的 DNS 没有给定主机的 IP 地址。然而,由于有一个有效的 IP 地址和最初给出的域名,可能没有连接到互联网:

$ nslookup mmsdemo.cloudapp.net
(...)

Non-authoritative answer:
Name:   mmsdemo.cloudapp.net
Address: 40.113.108.105

In other words, check that the internet connection is set up properly (on the machine that runs your code, and more specifically, that python can make use of it).

换句话说,检查互联网连接是否设置正确(在运行代码的机器上,更具体地说,python 可以使用它)。

Update

更新

The hostname given originally was as in my first answer. The new domain now in the question proofs my assumption was right:

最初给出的主机名与我的第一个答案一样。现在问题中的新域证明我的假设是正确的:

$ nslookup es_search_demo.com
(...)

** server can't find es_search_demo.com: NXDOMAIN

回答by AndreyF

Seems like your DNS server can't find an address associated to the URL you are passing

似乎您的 DNS 服务器找不到与您传递的 URL 关联的地址