0

I am new to python. I have read the documentation of requests module. I am working on validating the crt file of the website using python requests module. Instead of validating with the /etc/ssl/certs, i want to access it in the custom location. In my code, the crt file will be placed in the custom location. The Code will access the file and validates it. It works great for crt file of various https websites. But it fails on self signed ssl. In browser, the webpage(https://192.168.1.27/) gets opened. but it can't accessed via python request module. I have added the path in the code. It shows match error. How can i make that possible. I saw Validate SSL certificate using python But i can't solve it. Here's the sample code:

import requests
certpath = '/home/paulsteven/cert_check/Jeba.crt'
response = requests.get('https://192.168.1.27/', verify = certpath)

Error i got:

Traceback (most recent call last):
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/connection.py", line 364, in connect
    _match_hostname(cert, self.assert_hostname or server_hostname)
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/connection.py", line 374, in _match_hostname
    match_hostname(cert, asserted_hostname)
  File "/usr/lib/python3.5/ssl.py", line 301, in match_hostname
    % (hostname, dnsnames[0]))
ssl.CertificateError: hostname '192.168.1.27' doesn't match 'Jeba'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 376, in send
    timeout=timeout
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/paulsteven/.local/lib/python3.5/site-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='192.168.1.27', port=443): Max retries exceeded with url: / (Caused by SSLError(CertificateError("hostname '192.168.1.27' doesn't match 'Jeba'",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 480, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 588, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 437, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='192.168.1.27', port=443): Max retries exceeded with url: / (Caused by SSLError(CertificateError("hostname '192.168.1.27' doesn't match 'Jeba'",),))
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37

1 Answers1

0

First, let's understand the error. An SSL certificate has a specific hostname attached to it, in this case Jaba. In this line of your code:

response = requests.get('https://192.168.1.27/', verify = certpath)

You are sending an HTTP request with 192.168.1.27 as the host header, not Jaba. So, the goal is to send an HTTPS request to the server with IP address 192.168.1.27 with a Host header of Jaba.

This utility should allow you to do so with minimal effort.

MrName
  • 2,363
  • 17
  • 31