首页 > 解决方案 > Python:requests.exceptions.ConnectionError:HTTPSConnectionPool

问题描述

我正在尝试通过以下代码监视某些应用程序 URL,但它给了我错误。请提出这里的问题是什么?

代码:

#!/usr/bin/env python3

import requests
import os

f = open("sites.txt", 'r')
dns = f.readlines()
for obj in dns:
#    try:
        url = "https://" + obj
        print(obj)
        response = requests.get(url, verify=False, timeout=5)
        if response.status_code != 200:
            print("Site not rechable", url)

[user@server]$ cat sites.txt
www.google.com
web1.com

结果:

[user@server]$ python3 monitor.py
www.google.com

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 157, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/usr/local/lib/python3.6/site-packages/urllib3/util/connection.py", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/local/lib/python3.6/socket.py", line 745, 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.6/site-packages/urllib3/connectionpool.py", line 672, in urlopen
    chunked=chunked,
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 376, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 300, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f3cf8d221d0>: 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.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 720, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.google.com%0a', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f3cf8d221d0>: Failed to establish a new connection: [Errno -2] Name or service not known',))

我可以确认可以从服务器访问 google,并且我能够获得 curl 结果。

标签: pythonpython-3.x

解决方案


简单地说,第二个网站已经死了https://web1.com/,所以 requests 抛出异常。你需要拦截它:你需要检查它:

import requests
dns = ['www.google.com', 'web1.com']
for obj in dns:
        url = "https://" + obj
        try:
            response = requests.get(url, verify=False, timeout=5)
        except requests.exceptions.ConnectionError:
            print("Site not rechable", url)

推荐阅读