Python http.client for HTTPS Connection

This section provides a tutorial example on how to use the http.client Python module to connect to an HTTPS server.

If you want to write your own Python program to communicate with an HTTPS Web server, you should start the http.client module and the HTTPSConnection(host) class:

Here is my first Python script to verify HTTPS support provided by http.client and ssl modules:

#- http-client-HTTPS-test.py
#- Copyright (c) 2025 HerongYang.com. All Rights Reserved.

import sys
host = sys.argv[1]

import http.client
conn = http.client.HTTPSConnection(host)
conn.request("GET", "/")
res = conn.getresponse()

print(res.headers)
content = res.read(64)
print(content)

conn.close() 

Several methods and properties from the http.client module are used in the script:

Now let's run this script to connect to the Google HTTPS server, with the root CA certificate file on my Mac computer running Python 3.8:

herong$ python3 --version 
  Python 3.8.0

herong$ python3 http-client-HTTPS-test.py www.google.com
  Date: Thu, 30 Apr 2026 02:57:24 GMT
  Expires: -1
  Cache-Control: private, max-age=0
  Content-Type: text/html; charset=ISO-8859-1
  Content-Security-Policy-Report-Only: object-src 'none';base-uri ...
  Accept-CH: Sec-CH-Prefers-Color-Scheme
  P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
  Server: gws
  X-XSS-Protection: 0
  X-Frame-Options: SAMEORIGIN
  Set-Cookie: __Secure-STRP=AEEP7gL0a5p8tl7r2F36zZkFzdbUaZJgq2bJvTdKx...
  Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
  Accept-Ranges: none
  Vary: Accept-Encoding
  Transfer-Encoding: chunked

  b'<!doctype html><html itemscope="" itemtype="http://schema.org/We'

As you can see from the output, my Python script successfully connected the Google HTTPS server without any errors.

Visit https://docs.python.org/3/library/http.client.html for more details on the http.client module.

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with Google Chrome

 Using HTTPS with Mozilla Firefox

 Using HTTPS with Microsoft Edge

 Using HTTPS with Apple Safari

 Using HTTPS with IE (Internet Explorer)

 Android and Server Certificate

 iPhone and Server Certificate

 Windows Certificate Stores and Console

 RDP (Remote Desktop Protocol) and Server Certificate

 macOS Certificate Stores and Keychain Access

 Linux Certificate Stores and Tools

 Perl Scripts Communicating with HTTPS Servers

 PHP Scripts Communicating with HTTPS Servers

Python Scripts Communicating with HTTPS Servers

Python http.client for HTTPS Connection

 Modifying SSL Parameters in Python

 Retrieving SSLSocket Information in Python

 Dumping Server Certificate in Python

 Using pyOpenSSL for HTTPS Connection

 Java Programs Communicating with HTTPS Servers

 .NET Programs Communicating with HTTPS Servers

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Comodo Free Personal Certificate

 Digital Signature - Microsoft Word

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Archived Tutorials

 References

 Full Version in PDF/EPUB