Whilst attempting to access this site through requests, I receive:
('Connection aborted.', error(54, 'Connection reset by peer'))
I have also tried to access the site through mechanize and urllib, both failed. However cURL works fine (see end for code).
I have tried requests.get()
with combinations of parameters verify=True
,stream=True
and I have also tried a request with the cURL header.
I tried to move to urllib / Mechanize as alternatives but both gave the same error.
My code for requests is as follows:
import requests
import cookielib
url = "https://datamuster.marketdatasuite.com/Account/LogOn?ReturnUrl=%2fProfile%2fList"
header = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip,deflate,sdch',
'Accept-Language':'en-US,en;q=0.8',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36'
}
jar = cookielib.CookieJar()
s = requests.Session()
s.headers.update(header)
r = s.get(url, cookies=jar)
cURL test with headers:
$ curl -v -I -H "....Testing: Header...." https://datamuster.marketdatasuite.com/Account/LogOn?ReturnUrl=%2fProfile%2fList
* Hostname was NOT found in DNS cache
* Trying 54.252.86.7...
* Connected to datamuster.marketdatasuite.com (54.252.86.7) port 443 (#0)
* TLS 1.2 connection using TLS_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: datamuster.marketdatasuite.com
* Server certificate: COMODO SSL CA
* Server certificate: AddTrust External CA Root
> HEAD /Account/LogOn?ReturnUrl=%2fProfile%2fList HTTP/1.1
> User-Agent: curl/7.37.1
> Host: datamuster.marketdatasuite.com
> Accept: */*
> ....Testing: Header....
>
< HTTP/1.1 200 OK
This process wasn't totally straightforward so I thought I'd post a new answer to make it easy to follow for others.
Following this thread, I needed to install these libraries get SNI to work with Python 2:
However, pyOpenSSL may cause problems when installed with
pip install pyOpenSSL
. I actually had to remove my existing openssl, since pyOpenSSL version 0.14 didn't seem to work:The following command installed all necessary dependencies:
This should get requests to now work with SNI on python 2.
Keep reading for the issues with pyOpenSSL ver. 0.14...
When installing ver. 0.14 I get the following error:
and pyOpenSSL installs as ver. 0.14 incompletely:
as can be seen from the
requests.get()
attempt:The following commands revert to pyOpenSSL ver. 0.13 and correct the issue
and then in python:
I got the same issues when using Python requests to send testing requests to WordPress (I installed WordPress on a dedicate server). I tried to update SSL- packages without success.
Then, I realised that the requests sent to server got delays in receiving response. The long delays requests were always "kicked-off" and caused ('Connection aborted.', error(54, 'Connection reset by peer')). It turned out that the web server (apache) resets the connection while the request is still waiting for response.
I increase the KeepAliveTimeout from 5 seconds to 20 seconds (in Apache web server) and never get this error again.
Improve code for Exceptions: Increasing KeepAliveTimeout works in most of the tests. However, in some tests, I still got the same error and program stops. I add the code that catches Exception and repeat the request it occurs.
Hope this will help!
The server requires the use of SNI and just closes the connection if no SNI is used. Looks like curl uses SNI, while at least the version of the requests library you are using does not use SNI.
You can try this with OpenSSL. Without SNI you get an error:
But if you use SNI (
-servername ...
) then it works:According to the FAQ for request SNI is not support with Python 2, only with Python 3. See this resource for information on how to make SNI possible with Python 2.