简体   繁体   English

Python:requests.exceptions.ConnectionError:HTTPSConnectionPool

[英]Python: requests.exceptions.ConnectionError: HTTPSConnectionPool

I am trying to monitor some application URL via below code but it's giving me error.我正在尝试通过以下代码监视某些应用程序 URL ,但它给了我错误。 Please suggest what is the issue in here?请提出这里的问题是什么?

Code:代码:

#!/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

result:结果:

[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',))

I can confirm that google is reachable from the server and I am able to get the curl result.我可以确认可以从服务器访问 google,并且我能够获得 curl 结果。

Simply, the second website is dead https://web1.com/ , so requests is throwing an exception.简单地说,第二个网站已经死了https://web1.com/ ,所以 requests 抛出异常。 You need to intercept it: You need to check it:你需要拦截它:你需要检查它:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 requests.exceptions.ConnectionError Python - requests.exceptions.ConnectionError Python Python request.exceptions.ConnectionError:HTTPSConnectionPool:URL超过最大重试次数:[Errno 111]连接被拒绝) - Python requests.exceptions.ConnectionError: HTTPSConnectionPool : Max retries exceeded with url: [Errno 111] Connection refused) 意外的请求。异常。连接错误 - Unexpected requests.exceptions.ConnectionError requests.exceptions.ConnectionError:HTTPConnectionPool - requests.exceptions.ConnectionError: HTTPConnectionPool 每当我运行此代码时,它都会向我显示 requests.exceptions.ConnectionError: HTTPSConnectionPool 错误 - whenever i run this code its shows me the requests.exceptions.ConnectionError: HTTPSConnectionPool error 如何解决获取请求中的 requests.exceptions.ConnectionError - Python - how to solve requests.exceptions.ConnectionError in get request - Python python - 处理连接异常(requests.exceptions.ConnectionError) - python - handle connection exception (requests.exceptions.ConnectionError) requests.exceptions.ConnectionError [Errno 11004] getaddrinfo在Python中使用请求失败。 浏览器工作 - requests.exceptions.ConnectionError [Errno 11004] getaddrinfo failed using Requests in Python. Browser works POST API 请求如何在 python 中使用并行处理? requests.exceptions.ConnectionError: - how do POST API requests use parallel processing in python? requests.exceptions.ConnectionError: Python request.exceptions.ConnectionError:HTTPConnectionPool(host = &#39;10 .10.10.10&#39;,port = 80):URL超过了最大重试次数 - Python requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.10.10.10', port=80): Max retries exceeded with url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM