【问题标题】:Requests library raises urllib connection error请求库引发 urllib 连接错误
【发布时间】:2019-11-01 03:54:36
【问题描述】:

在尝试从我的网站下载图像时,请求库给了我一个我以前从未遇到过的错误。

这里是下载功能

def dl_jpg(url, filePath, fileName):                        
    fullPath = filePath + fileName + '.jpg'                 
    r = requests.get(url,allow_redirects=True)              
    open(fullPath,'wb').write(r.content)    

我输入的网址是: http://www.deepfrybot.ga/uploads/c4c7936ef4218dbe7014cb543049168b.jpg

这是错误信息

Last login: Fri Nov  1 03:36:32 2019 from 116.193.136.13
       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip ~]$ cd dfb-master
[ec2-user@ip dfb-master]$ sudo python3 mainr.py
enter url (n) if auto: http://www.deepfrybot.ga/uploads/c4c7936ef4218dbe7014cb543049168b.jpg
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 57, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib64/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 355, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python3.7/http/client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib64/python3.7/http/client.py", line 1290, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)  File "/usr/lib64/python3.7/http/client.py", line 1239, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 183, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fdce843ed50>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='www.deepfrybot.ga', port=80): Max retries exceeded with url: /uploads/c4c7936ef4218dbe7014cb543049168b.jpg (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fdce843ed50>: Failed to establish a new connection: [Errno -2] Name or service not known'))
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mainr.py", line 32, in wrapper
    return job_func(*args, **kwargs)
  File "mainr.py", line 138, in main_p
    dl_jpg(url, get_abs_file('images/'), file_name)
  File "mainr.py", line 67, in dl_jpg
    r = requests.get(url,allow_redirects=True)
  File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.7/site-
packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.deepfrybot.ga', port=80): Max retries exceeded with url: /uploads/c4c7936ef4218dbe7014cb543049168b.jpg (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fdce843ed50>: Failed to establish a new connection: [Errno -2] Name or service not known'))

我不知道为什么会发生这种情况,非常感谢您提供一点帮助

【问题讨论】:

  • 脚本无法解析名称“www.deepfrybot.ga”。您的 EC2 实例是否具有 DNS 访问权限?
  • 是的,.ga 域名基本上被认为是“危险的”,这就是亚马逊屏蔽 em 的原因。不过不用担心,我找到了克服它的方法!
  • @0subscriberswith0videos 您能否在回答中提及这一点? (然后接受!)另外,有没有这方面的公开信息?

标签: python-3.x python-requests urllib3


【解决方案1】:

lxop 说“

脚本无法解析名称“www.deepfrybot.ga”。做 您的 EC2 实例有 DNS 访问权限?

" 在检查和研究了一下之后,我了解到某些特定的 EC2 实例/urllib3 有时无法访问免费的顶级域(如 .ga、.ml、.tk),因为它们通常具有恶意的倾向并且因为他们通常缺乏 SSL 证书。 这些带有 SSL 证书的域可以正常工作! 如果你在说heliohost.org(我是)托管,只需将脚本中的域从yourwebsite.ga更改为yourwebsite.yourhost.domain 那应该解决它!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2021-12-23
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多