I've searched high and low and can't find a fix that seems to fit my use case. I've had some Python3 install issues on my M1 mac but think it's now running okay.
Just trying to learn / work with the requests library, but when I try to run this very simple python code:
import requests
response = requests.get("https://api.open-notify.org/this-api-doesnt-exist")
print(response.status_code)
I get the below error...
Any advice on how to fix this? I'm pulling my hair out.
Thank you!
/Users/myname/venv/python310/bin/python /Users/myname/Documents/2022_api/api01.py
Traceback (most recent call last):
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connection.py", line 358, in connect
conn = self._new_conn()
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x10f5a0850>: Failed to establish a new connection: [Errno 61] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/adapters.py", line 440, in send
resp = conn.urlopen(
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "/Users/myname/venv/python310/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /this-api-doesnt-exist (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x10f5a0850>: Failed to establish a new connection: [Errno 61] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myname/Documents/2022_api/api01.py", line 3, in <module>
response = requests.get("https://api.open-notify.org/this-api-doesnt-exist")
File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "/Users/myname/venv/python310/lib/python3.10/site-packages/requests/adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.open-notify.org', port=443): Max retries exceeded with url: /this-api-doesnt-exist (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x10f5a0850>: Failed to establish a new connection: [Errno 61] Connection refused'))
Process finished with exit code 1
Connection Refused means that the host (api.open-notify.org) is not listening on the port (https is on port 443) in your request.
I tried connecting to port 80 (plain http) instead and that worked for me:
>>> response = requests.get("http://api.open-notify.org/this-api-doesnt-exist")
>>> print(response.status_code)
404
For laughs, I tried connecting to the base url
>>> response = requests.get("http://api.open-notify.org")
>>> print(response.status_code)
500
>>> response
<Response [500]>
>>> response.text
'<html>\r\n<head><title>500 Internal Server Error</title></head>\r\n<body bgcolor="white">\r\n<center><h1>500 Internal Server Error</h1></center>\r\n<hr><center>nginx/1.10.3</center>\r\n</body>\r\n</html>\r\n'
Related
I suspected that an endpoint I have times out every so often, so I created a python script that sends a request on loop every 30 seconds and prints the responses it receives:
if __name__ == '__main__':
counter = 0
while True:
res = requests.post(url, payload)
try:
response = res.json()
except json.JSONDecodeError:
response = res.text
print(response)
if res.status_code != 201:
break
time.sleep(30)
print(res)
print(dir(res))
print(res.status_code)
print(response)
The script eventually failed with the following traceback:
{'success': True, 'created_on': '2022-09-08 22:42'} # --- 201 response
{'success': True, 'created_on': '2022-09-08 22:43'} # --- 201 response
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 353, in connect
conn = self._new_conn()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000001F5485BB9A0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='url', port=443): Max retries exceeded with url: /new_lead/5kbLWVNXHQ3U6lJ9trME8hPSf (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001F5485BB9A0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\dev\pythonProject\main.py", line 13, in <module>
res = requests.post(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='url', port=443): Max retries exceeded with url: /new_lead/5kbLWVNXHQ3U6lJ9trME8hPSf (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001F5485BB9A0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed'))
but now I'm a bit confused as to what this means. If it was the server timing out, wouldn't it give a 500 response and the script would have broke out of the while loop and tried to print the response objects? Does this mean that my internet connection dropped and the script was unable to make the request because the pc wasn't connected to the internet?
From the first traceback:
socket.gaierror: [Errno 11004] getaddrinfo failed
your DNS lookup failed. While that exception was being handled, all the other stuff failed, but that's the underlying problem.
I was trying to run this code:
import requests
r = requests.get("https://upos-sz-mirrorkodo.bilivideoo1.com/")
But a BIG ERROR has raised.
Traceback (most recent call last):
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\王子涵\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connection.py", line 353, in connect
conn = self._new_conn()
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000026E92F17F40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Programming\Python\VSC\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\util\retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='upos-sz-mirrorkodo.bilivideoo1.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026E92F17F40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "e:\Desktop\1.py", line 48, in <module>
r = get(
File "E:\Programming\Python\VSC\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='upos-sz-mirrorkodo.bilivideoo1.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026E92F17F40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
I tried to ping this host but failed, and Google didn't give useful advice.
Could you please tell me why and how to deal with it? Thanks a lot.
I have also asked this question on Github at https://github.com/psf/requests/issues/6010.
This error indicates that the URL you specified is simply not reachable. You said yourself that you cannot ping the host. Therefore, this issue is not related to your code. If you try requests.get("https://www.google.com/") it will probably work fine.
Update: Some comments pointed out that you might have a typo in your URL and that you meant to type:
upos-sz-mirrorkodo.bilivideo.com
I have tried to find the right answer for my situation, but it seems it may not exists yet.
In my current training course (very beginner level) I have an exercise to get some weather json info from a url (Open Weather in this case). Currently, this is the only way I have been taught to do this:
url = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+api_key
request = requests.get(url)
json = request.json()
print(json)
I have my "city" and "api_key" parameters set as instructed, and have been able to successfully run my code and get the json info when connected to my personal home network. However, when I connect to my work network, physically at the office or via VPN, I get this magical wall:
Traceback (most recent call last): File
"C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 169, in _new_conn
conn = connection.create_connection( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 96, in create_connection
raise err File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 86, in create_connection
sock.connect(sa) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because
connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 699, in urlopen
httplib_response = self._make_request( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 394, in _make_request
conn.request(method, url, **httplib_request_kw) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers) File
"C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line 1257,
in request
self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line
1303, in _send_request
self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line 1252,
in endheaders
self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line
1012, in _send_output
self.send(msg) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line 952, in
send
self.connect() File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 200, in connect
conn = self._new_conn() File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 181, in _new_conn
raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000255A4B11370>:
Failed to establish a new connection: [WinError 10060] A connection
attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because
connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\adapters.py",
line 439, in send
resp = conn.urlopen( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 755, in urlopen
retries = retries.increment( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\util\retry.py",
line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError:
HTTPConnectionPool(host='api.openweathermap.org', port=80): Max
retries exceeded with url:
/data/2.5/weather?q=*REDACTED*&appid=*REDACTED* (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x00000255A4B11370>: Failed to establish a new connection: [WinError
10060] A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"c:\Users\*REDACTED*\Training\Code\Python\weather.py", line 7, in
<module>
request = requests.get(url) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\api.py",
line 75, in get
return request('get', url, params=params, **kwargs)
return session.request(method=method, url=url, **kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\sessions.py",
line 542, in request
resp = self.send(prep, **send_kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\sessions.py",
line 655, in send
r = adapter.send(request, **kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\adapters.py",
line 516, in send
raise ConnectionError(e, request=request) requests.exceptions.ConnectionError:
HTTPConnectionPool(host='api.openweathermap.org', port=80): Max
retries exceeded with url:
/data/2.5/weather?q=*REDACTED*&appid=*REDACTED* (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x00000255A4B11370>: Failed to establish a new connection: [WinError
10060] A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond')) PS
C:\Users\*REDACTED*\Training\Code\Python&
C:/Users/*REDACTED*/Python/Python39/python.exe
"c:/Users/*REDACTED*/Training/Code/Python/weather.py" Traceback (most
recent call last): File
"C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 169, in _new_conn
conn = connection.create_connection( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 96, in create_connection
raise err File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\util\connection.py",
line 86, in create_connection
sock.connect(sa) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because
connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 699, in urlopen
httplib_response = self._make_request( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 394, in _make_request
conn.request(method, url, **httplib_request_kw) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers) File
"C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line 1257,
in request
self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line
1303, in _send_request
self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line 1252,
in endheaders
self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line
1012, in _send_output
self.send(msg) File "C:\Users\*REDACTED*\Python\Python39\lib\http\client.py", line 952, in
send
self.connect() File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 200, in connect
conn = self._new_conn() File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connection.py",
line 181, in _new_conn
raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000229035A1370>:
Failed to establish a new connection: [WinError 10060] A connection
attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because
connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\adapters.py",
line 439, in send
resp = conn.urlopen( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\connectionpool.py",
line 755, in urlopen
retries = retries.increment( File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\urllib3\util\retry.py",
line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError:
HTTPConnectionPool(host='api.openweathermap.org', port=80): Max
retries exceeded with url:
/data/2.5/weather?q=*REDACTED*&appid=*REDACTED* (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x00000229035A1370>: Failed to establish a new connection: [WinError
10060] A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"c:\Users\*REDACTED*\Training\Code\Python\weather.py", line 7, in
<module>
request = requests.get(url) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\api.py",
line 75, in get
return request('get', url, params=params, **kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\api.py",
line 61, in request
return session.request(method=method, url=url, **kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\sessions.py",
line 542, in request
resp = self.send(prep, **send_kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\sessions.py",
line 655, in send
r = adapter.send(request, **kwargs) File "C:\Users\*REDACTED*\Python\Python39\lib\site-packages\requests-2.26.0-py3.9.egg\requests\adapters.py",
line 516, in send
raise ConnectionError(e, request=request) requests.exceptions.ConnectionError:
HTTPConnectionPool(host='api.openweathermap.org', port=80): Max
retries exceeded with url:
/data/2.5/weather?q=*REDACTED*&appid=*REDACTED* (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x00000229035A1370>: Failed to establish a new connection: [WinError
10060] A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond'))
Quick ref of facts/actions taken:
-Tried setting the proxy globally to all Python instances via the set proxy... command.
-Not using a venv.
-Tried to add the --proxy... argument at the end of the command before executing via the the Visual Studio Code terminal.
-On Windows 10 Enterprise.
I am unable to move to the next module of the course because I need this code to work so that I know how to fix the error in the next set of codes I will have to do. I also apologize if this is a lot of words to get through (I am a noob for a reason, I guess), but I really appreciate anyone who could guide me.
For Requests proxy set in own manner Proxies in Requests
Thanks to the help of a colleague, I was able to better interpret the link that Сергей Зайков referenced. It was a proxy issue after all.
After making my adjustment in the code, I was able to modify it by adding the lines below and updating the request.get
http_proxy = "http://*REDACTED*:8080"
proxyDict = {"http" : http_proxy}
request = requests.get(url, proxies=proxyDict)
I am now able to run my code as intended.
Hi i'm trying to use different proxy to scrape this [website][1],
But i'm keep getting errors.
import requests
from bs4 import BeautifulSoup
import pandas as pd
import urllib3
url2= 'https://forecast.weather.gov/MapClick.php?lat=33.96746500000006&lon=-118.25679999999994#.XphNUsgzaUk'
proxies1 = {
"https": "https://209.220.97.131:8080",
"http": "http://209.220.97.131:8080"
}
page = requests.get(url2, proxies=proxies1, verify=False)
And this is the error i'm getting:
C:\Users\USER\PycharmProjects\untitled\venv\Scripts\python.exe "C:/Users/USER/PycharmProjects/Scraper/Clever scraper.py"
Traceback (most recent call last):
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\connectionpool.py", line 667, in urlopen
self._prepare_proxy(conn)
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\connectionpool.py", line 930, in _prepare_proxy
conn.connect()
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\connection.py", line 308, in connect
conn = self._new_conn()
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000001BED2B7E388>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\connectionpool.py", line 725, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='forecast.weather.gov', port=443): Max retries exceeded with url: /MapClick.php?lat=33.96746500000006&lon=-118.25679999999994 (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001BED2B7E388>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/Scraper/Clever scraper.py", line 17, in <module>
page = requests.get(url2, proxies=proxies1, verify=False)
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\USER\PycharmProjects\untitled\venv\lib\site-packages\requests\adapters.py", line 510, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='forecast.weather.gov', port=443): Max retries exceeded with url: /MapClick.php?lat=33.96746500000006&lon=-118.25679999999994 (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001BED2B7E388>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it')))
Process finished with exit code 1
For a while, i have been trying to use cloud9 to give me an extra hand on repetitive tasks on my job. However, the majority of those tasks have web scraping involved, and I am having a hard time using the urllib library.
It does not work even for simple codes. It keeps running forever, and I can't find the reasons for that. I will appreciate some tips...
from urllib.request import urlopen
import json
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = ('Enter - ')
html = urlopen(url, context=ctx).read()
str_data = open(html).read()
json_data = json.loads(str_data)
for entry in json_data:
name = entry[0];
title = entry[1];
print((name, title))
Without providing much debug information, one can only assume that you're probably timing out, since you're not providing the urlopen function with a timeout.
To avoid this, put a reasonable timeout to your urlopen function so that it doesn't run indefinitely.
Do yourself a favor and replace that with Requests. Your example simplifies to:
import requests
url = "..."
json_data = requests.get(url).json()
for entry in json_data:
name = entry[0]
title = entry[1]
print(name, title)
That also removes a whole awful lot of potential for mistakes from your code.
New code
import requests
import json
url = "-"
json_data = requests.get(url).json()
info = json.loads(json_data)
print('User count:', len(info))
print(info)
Debug Mode:
[IKP3db-g] 15:43:52,971733 - INFO - IKP3db 1.4.1 - Inouk Python Debugger for CPython 3.6+
[IKP3db-g] 15:43:52,972714 - INFO - IKP3db listening on 127.0.0.1:15471
[IKP3db-g] 15:43:53,003016 - INFO - Connected with 127.0.0.1:50936
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 157, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 84, in create_connection
raise err
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 74, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 672, in urlopen
chunked=chunked,
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 376, in _make_request
self._validate_conn(conn)
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 994, in _validate_conn
conn.connect()
File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 334, in connect
conn = self._new_conn()
File "/home/ubuntu/.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 0x7fa2bb9729e8>: Failed to establish a new connection: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/home/ubuntu/.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 "/home/ubuntu/.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='ot.cloud.mapsfinancial.com', port=443): Max retries exceeded with url: /pegasus/api/consulta/caixa/saldos/carteira/DVG1%20FIA/2018-01-08 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2bb9729e8>: Failed to establish a new connection: [Errno 110] Connection timed out',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/ikp3db.py", line 2047, in main
File "/usr/local/lib/python3.6/dist-packages/ikp3db.py", line 1526, in _runscript
exec(statement, globals, locals)
File "<string>", line 1, in <module>
File "/home/ubuntu/environment/Testing.py", line 5, in <module>
json_data = requests.get(url).json()
File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='ot.cloud.mapsfinancial.com', port=443): Max retries exceeded with url: xxxxxxxx (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2bb9729e8>: Failed to establish a new connection: [Errno 110] Connection timed out',))[IKP3db-g] 15:46:04,668520 - INFO - Uncaught exception. Entering post mortem debugging