Python requests throwing SSLError while using local certificate

2.1k views Asked by At

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?

0

There are 0 answers