
Python Script to Test if Website Support HTTP/2


Python is fast and known for its performance and capabilities.
While working on the HTTP/2 test tool, I tried many options and found Python was fastest, more accurate, and lightweight. If you are developing something in-house to test if the site supports HTTP/2 or not, you may use the following code.
It works on Python 3.x.
import socket
import ssl
import csv
import argparse
from urllib.parse import urlparse
socket.setdefaulttimeout(5)
headers = {"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"}
def check_http2(domain_name):
try:
HOST = urlparse(domain_name).netloc
PORT = 443
ctx = ssl.create_default_context()
ctx.set_alpn_protocols(['h2', 'spdy/3', 'http/1.1'])
conn = ctx.wrap_socket(
socket.socket(socket.AF_INET, socket.SOCK_STREAM), server_hostname=HOST)
conn.connect((HOST, PORT))
pp = conn.selected_alpn_protocol()
if pp == "h2":
return {"http2": True}
else:
return {"http2": False}
except Exception as e:
print(e)
parser = argparse.ArgumentParser()
parser.add_argument("domain", help="display a square of a given number",
type=str)
args = parser.parse_args()
print(check_http2(args.domain))
To run the program, you can use the following syntax.
python3 $SCRIPT.py $URL
Ex
[email protected]:~# python3 http2.py https://siterelic.com
{'http2': True}
[email protected]:~#
You see the few lines of code to achieve. If you are looking to master in Python, then check out this Udemy course. And, to take advantage of H2 protocol, refer the following implementation guide.
- Tagged in:
- Python
More great readings on Development
-
9 Top API Management Solution for a Small Business to EnterpriseAmrita Pathak on June 22, 2022
-
Software Development Life Cycle (SDLC): A Complete GuideAmrita Pathak on June 21, 2022
-
Application Load Balancer: Everything You Need to knowNaman Yash on June 20, 2022
-
Ultimate SQL Cheat Sheet to Bookmark for Later [2022]Butterfly Thoughts on June 21, 2022
-
How to Sort a Python Dictionary by Key or ValueBala Priya C on June 15, 2022
-
11 Deep Learning Software in 2022Amrita Pathak on June 15, 2022
Join Geekflare Newsletter
Every week we share trending articles and tools in our newsletter. More than 10,000 people enjoy reading, and you will love it too.