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

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

Communicating with HTTPS Servers

http.client for HTTPS Connection

 Modifying SSL Parameters

 Retrieving SSLSocket Information

 Dumping Server Certificate

 Use pyOpenSSL for HTTPS Connection

 tinyec - Tiny Library for ECC

 Generating EC Public-Private Keys

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB