How to connect with SSL to an HTTPS server in Python 2.7.10+ and make the certificate trusted?
I want to connect to an https server using ssl with certificate validation and hostname validation (don't suggest turning off validation as it works). How can I do it with this code?
import ssl
import socket
context = ssl.create_default_context()
context.check_hostname = True
context.verify_mode = ssl.CERT_REQUIRED
conn = context.wrap_socket(socket.socket(socket.AF_INET),
server_hostname='adwords.google.com')
conn.connect(('adwords.google.com', 443))
Instead of connecting, there is an exception:
Traceback (most recent call last):
File "C:/Users/Crezary Wagner/PycharmProjects/learn-adwords/src/03_ssl.py", line 16, in <module>
conn.connect(('adwords.google.com', 443))
File "C:\root\Python27\lib\ssl.py", line 844, in connect
self._real_connect(addr, False)
File "C:\root\Python27\lib\ssl.py", line 835, in _real_connect
self.do_handshake()
File "C:\root\Python27\lib\ssl.py", line 808, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
Process finished with exit code 1
It's clear to me that a certificate cannot be verified as trusted, but how do I make that certificate trusted in Python 2.7.10+ and connect?
I want to set it up on the system in the wrong context, if possible.
+3
Chameleon
source
to share
1 answer
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
response = requests.get('https://adwords.google.com', verify=False)
print response.content
0
iNoob
source
to share