Connection timeout error in response.get() - python

I am learning about creating proxy servers. i tried to create a script by watching videos of a guy teaching python. when he creates a script and runs that script it's work fine one his pc. when i try to create the exact same script and run it i get error of connection timeout. I don't know why.
The error that i am getting every single time when i run the code
[Running] python -u "e:\Xampp\htdocs\trafficBot2\main.py"
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\site-packages\urllib3\connectionpool.py", line 700, in urlopen
self._prepare_proxy(conn)
File "C:\Program Files\Python310\lib\site-packages\urllib3\connectionpool.py", line 996, in _prepare_proxy
conn.connect()
File "C:\Program Files\Python310\lib\site-packages\urllib3\connection.py", line 364, in connect
self.sock = conn = self._connect_tls_proxy(hostname, conn)
File "C:\Program Files\Python310\lib\site-packages\urllib3\connection.py", line 499, in _connect_tls_proxy
socket = ssl_wrap_socket(
File "C:\Program Files\Python310\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "C:\Program Files\Python310\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "C:\Program Files\Python310\lib\ssl.py", line 513, in wrap_socket
return self.sslsocket_class._create(
File "C:\Program Files\Python310\lib\ssl.py", line 1071, in _create
self.do_handshake()
File "C:\Program Files\Python310\lib\ssl.py", line 1342, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\site-packages\requests\adapters.py", line 489, in send
resp = conn.urlopen(
File "C:\Program Files\Python310\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
retries = retries.increment(
File "C:\Program Files\Python310\lib\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='ipinfo.io', port=443): Max retries exceeded with url: /json (Caused by ProxyError('Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#https-proxy-error-http-proxy', SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)'))))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "e:\Xampp\htdocs\trafficBot2\main.py", line 10, in <module>
response = requests.get("https://ipinfo.io/json", proxies=proxies)
File "C:\Program Files\Python310\lib\site-packages\requests\api.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "C:\Program Files\Python310\lib\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files\Python310\lib\site-packages\requests\sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "C:\Program Files\Python310\lib\site-packages\requests\sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "C:\Program Files\Python310\lib\site-packages\requests\adapters.py", line 559, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='ipinfo.io', port=443): Max retries exceeded with url: /json (Caused by ProxyError('Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#https-proxy-error-http-proxy', SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)'))))
here is the screenshot from the video of the guy that has written this script and it worked for him, but not for me. i don't know why i have done same thing that he does but still get error.
My code.
import requests
proxies = {
'https': 'https://169.57.1.85:8123'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
response = requests.get("https://ipinfo.io/json", proxies=proxies)
print(response.json()['country'])

Related

SSL: WRONG_VERSION_NUMBER even ssl version attached to python is correct [duplicate]

When sending a request with authentication, I get a requests.exceptions.SSLError error which you can See below.
proxies = { 'https' : "http://user:pass#ip:port/" }
url = "https://httpbin.org/ip"
numberResponse = requests.get(url,proxies=proxies).text
print(numberResponse)
The requests.exceptions.SSLError
Traceback (most recent call last):
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 496, in _connect_tls_proxy
return ssl_wrap_socket(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 428, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 472, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1125)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 573, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/K_Yuk/OneDrive/Desktop/Gmail generator/test.py", line 15, in <module>
numberResponse = requests.get(url,proxies=proxies).text
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)')))
So then I tired verify=False as one of the requests.get() parameters but then get a requests.exceptions.ProxyError error which you can see below :
proxies = { 'https' : "http://user:pass#10.10.1.10:3128/"}
url = "https://httpbin.org/ip"
numberResponse = requests.get(url,proxies=proxies,verify=False).text
print(numberResponse)
The requests.exceptions.ProxyError
Traceback (most recent call last):
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 359, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 496, in _connect_tls_proxy
return ssl_wrap_socket(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 428, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 472, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
FileNotFoundError: [Errno 2] No such file or directory
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 573, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by ProxyError('Cannot connect to proxy.', FileNotFoundError(2, 'No such file or directory')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/K_Yuk/OneDrive/Desktop/Gmail generator/test.py", line 15, in <module>
numberResponse = requests.get(url,proxies=proxies,verify=False).text
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\K_Yuk\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 510, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by ProxyError('Cannot connect to proxy.', FileNotFoundError(2, 'No such file or directory')))
I tired to look every for the answer but nothing seems to work. I can't send a request with a proxy with
authentication. Any ideas?
The problem is very likely not the authentication. Unfortunately, you don't provide details of the proxy configuration and the URL you use for the proxy. The only thing you provide is:
proxies = { 'https' : eampleIpWithAuth }
Based on the reference to _connect_tls_proxy in the stacktrace the eampleIpWithAuth is very likely something like https://..., i.e. you try to access the proxy itself over HTTPS. Note that accessing a proxy over HTTPS is different from using a HTTP proxy for HTTPS. When accessing a HTTPS URL over a HTTPS proxy one essentially does double encryption to the proxy:
client --- [HTTPS wrapped inside HTTPS] --- proxy --- [HTTPS] --- server
Whereas with a HTTPS URL over a "normal" HTTP proxy there is only single encryption, i.e. it looks (simplified) like this:
client --- [HTTPS wrapped inside HTTP] --- proxy --- [HTTPS] --- server
Very likely the proxy you want to use is a plain HTTP proxy, and not a HTTPS proxy. This is actually the most common case.
The error happens since the proxy is not able to speak TLS but gets accessed by TLS. The fix is to use http://proxy and not https://proxy as the proxy address. Note that the latter worked in older versions of Python since proxy over HTTPS was not supported and a value of https:// for the protocol was treated the same as http://.
I had a similar problem with my script in python 3.9 version.
When I try to get some data from web server, I had exception like Your:
urllib3.exceptions.MaxRetryError
Simple add this line to configure Your requests
import requests
import urllib3
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:#SECLEVEL=1'
I hope, this help You to solve Your issue

Python3.6 - requests exceptions SSLError

I am having a problem with python3 script, that uses requests to perform API Get Request.
The problem is that the communication should go through proxy, i had similar issue using python3.6 -m pip install package_name, and figured it out by defining http_proxy and https_proxy to use "http://proxy_ip:port", however trying to do the same in the python script keeps failing, because the target destination only allow https communication, and routing the traffic through the http_proxy wont work in this case, but the https_proxy should be working fine from my browser, however when running the request get the below error:
Traceback (most recent call last):
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 700, in urlopen
self._prepare_proxy(conn)
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 994, in _prepare_proxy
conn.connect()
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/connection.py", line 364, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/connection.py", line 507, in connect_tls_proxy
ssl_context=ssl_context,
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/util/ssl.py", line 453, in ssl_wrap_socket
ssl_sock = ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/util/ssl.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/ssl.py", line 817, in init
self.do_handshake()
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:852)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/integration/.local/lib/python3.6/site-packages/requests/adapters.py", line 450, in send
timeout=timeout
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 786, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/home/integration/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='ruh-services.sec.ibm.com', port=443): Max retries exceeded with url: /micro/ticket_detail (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:852)'),))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/home/integration/.local/lib/python3.6/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/home/integration/.local/lib/python3.6/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/home/integration/.local/lib/python3.6/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/home/integration/.local/lib/python3.6/site-packages/requests/sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "/home/integration/.local/lib/python3.6/site-packages/requests/adapters.py", line 517, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='ruh-services.sec.ibm.com', port=443): Max retries exceeded with url: /micro/ticket_detail (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:852)'),))
below is the python script i am using:
import requests
url="https://any_website"
response = requests.get(url, headers={"User-Agent": "Opera/9.80 (X11; Linux x86_64; U; de) Presto/2.2.15 Version/10.00"}, auth = ("UserName", "Password"), proxies={"https": "https://proxy_ip:port"})
note * the error message is the same for all destinations, as i barely get the error immediately after running the script"
Appreciate your support to identify the cause of such problem.
Thanks

requests.exceptions.SSLError: HTTPSConnectionPool How can I solve it?

I got error:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
self._prepare_proxy(conn)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
conn.connect()
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\connection.py", line 364, in connect
conn = self._connect_tls_proxy(hostname, conn)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\connection.py", line 501, in _connect_tls_proxy
socket = ssl_wrap_socket(
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 512, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1070, in _create
self.do_handshake()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1341, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:997)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\urllib3\util\retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='ipinfo.io', port=443): Max retries exceeded with url: /json (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:997)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\PythonLesson\text.py", line 7, in <module>
response = requests.get('https://ipinfo.io/json', proxies=proxies)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\user\PycharmProjects\PythonLesson\venv\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='ipinfo.io', port=443): Max retries exceeded with url: /json (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:997)')))
Process finished with exit code 1
Do I need to download any pip SSL? Or is it because of a proxy problem? Or add a pip to IDE like "Pycharm"?
I get every time error even when I change a proxy.
my code:
import requests
proxies = {
'https': 'https://150.129.148.99:35101'
}
response = requests.get('https://ipinfo.io/json', proxies=proxies)
print(response.json()['country'])
print(response.json()['region'])
This is a video https://www.youtube.com/watch?v=nnIye9pM22w in min 5:21 that I copied his code, and just for him it works and for me it does not
It's definitely a problem with proxy.
You can check it with curl like this:
curl -v -x https://150.129.148.99:35101 ipinfo.io
You'll have:
* Trying 150.129.148.99...
* TCP_NODELAY set
And nothing more. That means the proxy server doesn't respond correctly.

Python request.get fails on specific site

I just finished automate the boring stuff with python, now i'm just practicing.
I try running the code below but it fails . Other sites has no issue connecting .
Searched for other questions they say add user:agent, it works for amazon.com but not for this site .
I tried allow_redirects = False and ,verify=False too , they are not working
I am using Windows 10 , Python 3.8.5 , requests 2.24.0
my code ;
import requests
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
print('1')
res = requests.get('https://freida.ama-assn.org/search/list?spec=42931',headers=headers)
print('2')
print(res.status_code)
res.raise_for_status()
This code does not prints '2' fails at requests.get
Error code as follows :
C:\Users\User198\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/User198/PycharmProjects/pythonProject/amaResidency.py
1
Traceback (most recent call last):
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
conn.connect()
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connection.py", line 362, in connect
self.sock = ssl_wrap_socket(
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\ssl_.py", line 384, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\User198\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\User198\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\User198\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
retries = retries.increment(
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\retry.py", line 403, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
raise value.with_traceback(tb)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
conn.connect()
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connection.py", line 362, in connect
self.sock = ssl_wrap_socket(
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\ssl_.py", line 384, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\User198\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\User198\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\User198\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError(0, 'Error'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/User198/PycharmProjects/pythonProject/amaResidency.py", line 6, in <module>
print(res.status_code)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\User198\PycharmProjects\pythonProject\venv\lib\site-packages\requests\adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error'))
So, seems like i fixed the issue .
In my case code was working via my cellphone internet but Wi-Fi connection was failing for this particular site .
I unchecked "Microsoft LLDP Protocol Driver" from Control panel => network and internet => network connections => LeftClick your connection and properties => Networking tab => Uncheck "Microsoft LLDP Protocol Driver"
Everything was checked except "microsoft network adapter multiplexor protocol"
Now "Microsoft LLDP Protocol Driver" is not checked too.
This solution worked for me
You may try ignoring SSL certificate verification using verify = False. It drastically reduced my count of similar ConnectionErrors.
res = requests.get('https://freida.ama-assn.org/search/list?spec=42931', headers=headers, verify=False)
Note that when verify is set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks.

Unable to perform API login using Python Request and post api calls

I'm trying to perform a login authentication using python-requests.
Explaination
I'm trying to access an API call of a website with AJAX Authentication. Hence I log in to the website using username and password and the is login_url(AJAX API URL). Then trying to use the cookies of login response for my next API request. As you have seen in the code in the next I'm using the cookie of login response as the header for the next API call.
I hope someone could help me to identify the issue in my request.
For reference my code is
from requests import Session
import requests
class Login:
def sendRequestWithAuthentication(self,loginDetails,requestDetails):
user_agent = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'}
action_url=loginDetails['action_url'] if 'action_url' in loginDetails.keys() else None
pay_load=loginDetails['login_details'] if 'login_details' in loginDetails.keys() else None
session_requests = requests.session()
if action_url and pay_load:
act_resp=session_requests.post(action_url, data=pay_load, headers=user_agent)
auth_cookies=act_resp.cookies.get_dict()
url,method,request_payload = requestDetails['url'],requestDetails['method'],requestDetails['payload']
querystring=requestDetails['querystring']
headers={**user_agent,**requestDetails['headers']}
if method == 'GET' or method == 'get':
response=session_requests.get(url,headers=headers,cookies=auth_cookies,data=request_payload,params=querystring)
elif method == 'POST' or method == 'post':
response=session_requests.get(url,headers=headers,cookies=auth_cookies,data=request_payload,params=querystring)
return response.json()
For this, I'm getting the following error
Traceback (most recent call last):
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn
conn.connect()
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\connection.py", line 361, in connect
self.sock = ssl_wrap_socket(
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\david\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\david\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\david\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\connectionpool.py", line 724, in urlopen
retries = retries.increment(
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='sso1.nbnco.net.au', port=443): Max retries exceeded with url: /F5Networks-SSO-Req?SSO_ORIG_URI=aHR0cHM6Ly9pcGFjdC5uYm5jby5uZXQuYXUvaXBhY3QvaW5kZXgvZG9sZKKExvZ2lu (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ValidateAPI.py", line 181, in <module>
z=ValidateAPI()
File "ValidateAPI.py", line 17, in __init__
self.getActivity(sys.argv[1].upper())
File "ValidateAPI.py", line 37, in getActivity
self.getTestCase(main_test,sub_test)
File "ValidateAPI.py", line 62, in getTestCase
data=self.sendRequestWithAuthentication(t_case['login_credentials'],api_param)
File "C:\ACCELARATE\API\API_LATEST\API\Login.py", line 18, in sendRequestWithAuthentication
act_resp=session_requests.post(action_url, data=pay_load, headers=user_agent)
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\sessions.py", line 578, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\sessions.py", line 665, in send
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\sessions.py", line 665, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\sessions.py", line 237, in resolve_redirects
resp = self.send(
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\david\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.8\RequestsVenv\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='sso1.organization.net.us', port=443): Max retries exceeded with url: /F5Networks-SSO-Req?SSO_ORIG_URI=aHR0cHM6Ly9pcGFjdC5uYm5jby5uZXQuYXHUvaXBhY3QvaW5kZXgvZG9sZExvZ2lu (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
UPDATE
The website login is done through multi factor authentication
I think the first issue that you should address is that requests won't trust the cert of that site, like the last error line states. Maybe try to add the cert first. Check here for how to set the cert.
Sorry for the short answer. I would've written a comment, but I'm still new and am therefore not allowed to comment.

Categories

Resources