Python requests throwing SSLError while using local certificate - python

I'm writing a script to a server and verify it's certificate. If I write the script using sockets it works fine:
import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s,
ca_certs="mule.crt",
cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect(('localhost', 8081))
message = "GET /api HTTP/1.1\r\nHost:localhost\r\n\r\n"
ssl_sock.write(bytes(message, 'UTF-8'))
data = ssl_sock.read().decode("utf-8")
print(data)
ssl_sock.close()
If I try to do a similar thing using requests:
import requests
r = requests.get('https://localhost:8081/api', cert=r'C:\path\to\cert\mule.crt')
print(r.status_code)
print(r.text)
This throws:
Traceback (most recent call last):
File "C:\path\to\py\ssltest.py", line 4, in <module>
r = requests.get('https://localhost:8081/api', cert=r'C:\path\to\cert\mule.crt')
File "C:\Python34\lib\site-packages\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "C:\Python34\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Python34\lib\site-packages\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python34\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python34\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2536)
Any ideas what I might be doing wrong?

Related

Python requests throwing SSLError bad handshake with proxy connection

In Python Requests module, using proxy connection to browse some url
I added proxy ip and port to https_proxy and http_proxy environment variables to route the traffic to proxy
import os
import requests
os.environ["HTTPS_PROXY"] = "proxy1.xx.local:8081"
os.environ["HTTP_PROXY"] = "proxy1xx.local:8081"
H = {"X-Authenticated-User": "bharani#dummy.com"}
url = r"https://google.co.in"
r = requests.get(url,headers=H,verify=False)
print r.status_code
print r.text
Response I'm getting from above code
Traceback (most recent call last):
File "pilot.py", line 356, in <module>
r = requests.get(url,headers=H,verify=False)
File "C:\Python27\lib\site-packages\requests\api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)
pyopenssl, ndg-httpsclient, pyasn1 are also installed.
Not sure What I'm missing.

Python script to pull data from RESTAPI fails with error

When I run my Python script to get data from RESTAPI of an application, I get the following error. I installed PIP and I installed requests package for the python. Here is my below query:
./simpleRunQuery.py <args> <args>
Traceback (most recent call last):
File "./simpleRunQuery.py", line 25, in <module>
res = requests.post(url, auth=(args.username, args.password), data=jsonRequest, headers=headers)
File "/Library/Python/2.7/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))
I can attach the script, but my script doens't have the lines 112 or 58 or any of these, it's a simple RESTAPI Script that queries and posts results here. Any pointers?

gaierror-2,Name or service name not known) when using requests.post in python

I am trying to send web request to my php code via linux running on raspberry pi. I am using putty to access the OS. When I am running the code, I am getting gai error-2. Below is my code:
import requests
values = {'"firstname":' "abc ", '"lastname":' "xyz"}
r = requests.post('http://rts.msmq.site/security.php', data=values)
I am unable to figure out the problem. Can someone let me know what could be the issue? I am using linux.
Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
gaierror stands for get_address_info_error, which means urllib is having trouble reaching the hostname 'http://rts.msmq.site/security.php', check that you can nslookup rts.msmq.site or dig rts.msmq.site and ping rts.msmq.site

How to fix "bad handshake" SSLErrors when utilizing python requests

I'm trying to get access to the BambooHR API (documentation here), but I receive the following error
params = {
'user': username,
'password': password,
'api_token': api_key}
url = 'https://api.bamboohr.com/api/gateway.php/company/v1/login'
r = requests.get(url, params=params)
Error:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/chriscruz/Dropbox/PycharmProjects/082716_r2/Shippy/API/bamboo_api2.py", line 31, in <module>
BambooFunctions().login()
File "/Users/chriscruz/Dropbox/PycharmProjects/082716_r2/Shippy/API/bamboo_api2.py", line 26, in login
r = requests.get(url, params=params, auth=HTTPBasicAuth(api_key, 'api_token'))
File "/Library/Python/2.7/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'SSL23_GET_SERVER_HELLO', 'unknown protocol')],)",)
I'm unsure what this is caused by as I've re-installed OpenSSL, Requests, and not sure how to fix this issue.
You try by setting verify=False, use this option if you are using self-signed certificates.
r = requests.get(url, params=params, verify=False)
More info http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

Sign in to twitter with python

I want to sign in to twitter with the code below and scrape twitter's data then:
import requests
import urllib2
with requests.Session() as c:
url = "https://twitter.com/login"
USER = "hadishamgholi74#gmail.com"
PASS = "52518685251868"
c.get(url)
login_data = {"session[username_or_email]": USER, "session[password]": PASS, "authenticity_token": "4d1c2137136cb297b3e83e382b0026d9213fe731", "scribe_log": "", "redirect_after_login": "", "authenticity_token": "4d1c2137136cb297b3e83e382b0026d9213fe731"}
c.post(url,data = login_data,headers={"Referer":"https://twitter.com"})
page = c.get("https://twitter.com")
print page.content
But it rise this error:
Traceback (most recent call last):
File "C:/Users/Mehdi/PycharmProjects/scrap/login1.py", line 9, in <module>
c.get(url)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 473, in get
return self.request('GET', url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:581)
What should i do?
According to this post, there is not support for the https protocol yet requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

Categories