SSL error using python(2.7) requests - python

I am unable to issue a request to piratebay using requests with python2.7. I did the same with python3.4 and it worked ok. The line which I'm trying to execute:
r = requests.get("http://thepiratebay.se/browse/201", verify=False)
I did the verify=False to try and escape all the SSL jargon to no avail. It's a small personal project anyway..
I also tried to change the version of SSL using this link, however it still is giving me
requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error.`
Thanks

The site thepiratebay.se requires Server Name Indication (SNI) and will throw an alert if the client does not support it. While python3 supported SNI for a while already with python2.7 SNI was only added with version 2.7.9. My guess is that you are using an older version of python 2.7 and that's why run into this error.

Related

How to find out what this error means: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1129)

From a python app attempting to make an HTTPS request I'm hitting an error which I'm struggling to understand:
[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1129)
I presume this is coming from OpenSSL. But besides the handshake failing, it doesn't tell me what the problem is. IE: it doesn't tell me why the handshake failed or what step of it failed.
Various links on google give things to try, but give no reason why they might work. I have tried them but nothing worked so far. Based on these results I have tried:
upgrading certifi
running the certificate install script in mac python install directory
upgrading the python version
Is there any way to get more information about this error?
Steffen Ullrich pointed out that this error is server-side. This may be useful to others debugging. For us the problem was that we were not sending a client certificate when we were supposed to be.
For now, just a work around - folks on this fourm led me to try
compare openssl ciphers (identical in both my good and bad environments)
openssl s_client -connect news.somewhere.com:563 (worked)
then add the following python code based on output from the above:
import ssl
cntxt = ssl.create_default_context()
cntxt.set_ciphers("AES256-GCM-SHA384")
import nntplib
nntp = nntplib.NNTP_SSL('news.somewhere.com', ssl_context=cntxt)
which solved the SSLV3_ALERT_HANDSHAKE_FAILURE I was getting. (You will want to select the cipher that worked in that s_client test connection to your server.)
There are a dozen library and module version differences between my two environments, but in general the old environment uses python-3.9.7 and the new (broken but now running environment) uses python-3.10.5
Hopefully this narrows things down a bit.

ServerSelectionTimeoutError when connecting to aws with pymongo

I am attempting to connect to MongoDB hosted on an AWS instance with a key file. I am able to ssh into the instance and connect to the database with no issues. When I try to connect to the database from a remote location with pymongo I receive this error:
ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol
Port 27017 is open and the source is set to 0.0.0.0/0.
from pymongo import MongoClient
client = MongoClient('mongodb://ec2-123-45-678-910.compute-1.amazonaws.com',
27017,
ssl=True,
ssl_keyfile='/path_to/mykey.pem')
db = client.test
coll = db.foo
coll.insert_many(records)
ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:645)
This question is nearly identical to mine, however the error is different and the solution posted there does not apply to my issue.
The address and key here have been changed, I have been going in circles on this for hours with no luck, any help would be appreciated.
This issue can cause because of following issue:
version of pymongo (suggest to use 3.3.0, which worked for me)
It can be a DNS issue, etc, in fact you could check for a DNS issue using:
telnet xx.xx.xx.xx port
can be a firewall issue
Can be an issue with ssl key. Try the following to test:
import os
import pymongo
import ssl
URL="url:port/db?ssl=true"
client = pymongo.MongoClient(URL, ssl_cert_reqs=ssl.CERT_NONE)
db = client.get_default_database()
print db
print db.collection_names()
I had the same problem (SSL handshake) with Pymongo module to connect to DocumentDB Azure (Data Base).
The error :
ServerSelectionTimeoutError: SSL handshake failed: EOF occurred in violation of protocol (_ssl.c:590)
I was using pymongo==3.4.0
To resolve this :
Change the version of pymongo by installing the 3.3.0 version
pip install pymongo==3.3.0
Try:
import pymongo
pymongo.__version__
For me, the problem was that my Python setup only supported TLS 1.0 – not TLS 1.1 or above.
You can check it like this:
Python 3
> from urllib.request import urlopen
> urlopen('https://www.howsmyssl.com/a/check').read()
Python 2
> from urllib2 import urlopen
> urlopen('https://www.howsmyssl.com/a/check').read()
Check the output for the key tls_version. If it says TLS 1.0 and not TLS 1.1 or TLS 1.2 that could be the problem.
If you're using a virtualenv, be sure to run the command inside.
Solution: Install Python with a newer version of OpenSSL
In order support TLS 1.1 or above, you may need to install a newer version of OpenSSL, and install Python again afterwards. This should give you a Python that supports TLS 1.1.
The process depends on your operating system – here's a guide for OS X.
virtualenv users
For me, the Python outside of my virtualenv had TLS 1.2 support, so just I removed my old virtualenv, and created a new one with the same packages and then it worked. Easy peasy!
See also:
The warning about TLS 1.0 in the Python 3 section in the PyMongo documenation. Although it's under the Python 3 section it also applies to Python 2
I had the same issue and talked for 30 minutes with the Mongo Atlas support which deployed over AWS. I run the next terminal command:
/Applications/Python\ 3.6/Install\ Certificates.command
I had the same issue. Please check if you are connected via VPN. when I disconnected it resolved my problem.

<urlopen error [Errno 1] _ssl.c:510: error:14077417:SSL

Does anyone know why I am getting this error?
SSLError: [Errno 1] _ssl.c:510: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1
I get the erro when using requests or urllib2, I'm running the code on Kodi. The code runs fine when I run it on Visual Studio on my PC.
I am trying to scrape a website that is blocked by my ISP, so I'm using a proxy version of the site.
import requests
url = 'https://kickass.unblocked.pe/'
r = requests.get(url)
The site is hosted by Cloudflare Free SSL and requires support for Server Name Indication (SNI). SNI is support with Python 2.7 only since version 2.7.9. I guess that you are using an older version.
verify=False (which is usually a bad idea anyway) will not help here because without SNI the handshake will fail because the server does not know which certificate is requested and thus will not sent any certificate but instead an alert.

Python requests - get request to secured resource - SSL3_GET_RECORD:decryption failed or bad record mac

I am using python-requests to perform get requests to some resources.
In staging and production environments things works out fine, but in the test environment with a slightly different setup, I receive the message below when trying to perform the request:
requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac
I have tried using an adapter as specified here: SSL Error on Python GET Request
I have tried all the protocols in the list. Still no luck.
I have tried mounting both the complete url and the domain url. No difference.
Any ideas on what to try next?

Python liburl2 timeout; can ping server fine, and wget works fine;

I'm trying to use Python's liburl2 to access the Dreamhost API documented here: http://wiki.dreamhost.com/API
Here is my code:
request = urllib2.Request('https://api.dreamhost.com/?key=<key>')
response = urllib2.urlopen(request)
page = response.read()
print(page)
This invariably fails with the error:
urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>
I'm absolutely stumped, because I can ping api.dreamhost.com just fine, and wget https://api.dreamhost.com/?key= works fine, too.
Any ideas?
I know it's an old question, but I faced the same problem, and found the solution through two other questions.
This, that shows me the problem is with the handshake using SSLv3:
OpenSSL issues in Debian Wheezy
And this, that gives some possible solutions:
Python HTTPS requests (urllib2) to some sites fail on Ubuntu 12.04 without proxy

Categories