HTTPS connection Python HTTPS connection Python python python

HTTPS connection Python


Python 2.x: docs.python.org/2/library/httplib.html:

Note: HTTPS support is only available if the socket module was compiled with SSL support.

Python 3.x: docs.python.org/3/library/http.client.html:

Note HTTPS support is only available if Python was compiled with SSL support (through the ssl module).

#!/usr/bin/env pythonimport httplibc = httplib.HTTPSConnection("ccc.de")c.request("GET", "/")response = c.getresponse()print response.status, response.reasondata = response.read()print data# => # 200 OK# <!DOCTYPE html ....

To verify if SSL is enabled, try:

>>> import socket>>> socket.ssl<function ssl at 0x4038b0>


To check for ssl support in Python 2.6+:

try:    import sslexcept ImportError:    print "error: no ssl support"

To connect via https:

import urllib2try:    response = urllib2.urlopen('https://example.com')     print 'response headers: "%s"' % response.info()except IOError, e:    if hasattr(e, 'code'): # HTTPError        print 'http error code: ', e.code    elif hasattr(e, 'reason'): # URLError        print "can't connect, reason: ", e.reason    else:        raise


import requestsr = requests.get("https://stackoverflow.com") data = r.content  # Content of responseprint r.status_code  # Status code of responseprint data