python - 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 结果。
解决方案
简单地说,第二个网站已经死了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)
推荐阅读
- laravel - Laravel Passport 将 exp、iat 和 nbf 的类型更改为 int 或 float
- c# - 尝试与组织断开连接时 Xero 中的 403 身份验证失败
- powershell - Two arrays, replacing the ID in one with the description of the other (matching ids) Powershell
- java - 如何将数据库添加到不同计算机中具有不同实例的 java 应用程序
- javascript - 解密和加密算法很时髦
- android - Android渐变边框?
- html - 如何在导航栏的左侧或中心添加徽标?
- angular - 在角度 9 中检查后,表达式发生了变化
- sql - 我应该总是更喜欢 EXISTS 而不是 SQL 中的 COUNT() > 0 吗?
- javascript - 如何使用 jQuery 或 JavaScript 将数据对象传递给引导模型