Created
May 10, 2020 15:06
-
-
Save dev-ritik/99609f5282a7cad5d13f621cd80431fe to your computer and use it in GitHub Desktop.
Simple HTTPS client example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import socket | |
import ssl | |
from urllib3.util.ssl_ import create_urllib3_context, resolve_ssl_version | |
# example.com IP address | |
ADDRESS = '93.184.216.34' | |
PORT = 443 | |
# HTTP request | |
message = """\ | |
GET / HTTP/1.1 | |
Host: example.com | |
""" | |
# Context for SSL Handshake | |
context = create_urllib3_context( | |
ssl_version=resolve_ssl_version(None), | |
cert_reqs=ssl.VerifyMode.CERT_REQUIRED) | |
# Root CA certificates for example.com | |
context.load_verify_locations('/home/ritik/Videos/example_root_CA.crt', None) | |
# Simple TCP Socket specification | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Initialize SSL socket | |
conn = context.wrap_socket(s, server_side=False) | |
# TCP + TLS Handshake | |
conn.connect((ADDRESS, PORT)) | |
# SSL secured connection established | |
print("SSL established. Peer: {} \n".format(conn.getpeercert())) | |
# Send the request | |
print("Requesting for home page") | |
conn.send(message.encode()) | |
print("Request sent, Waiting for response") | |
# Wait for server to respond | |
server_message = conn.recv(1024) | |
# print received message | |
print("Received message: \n{}".format(server_message.decode())) | |
# Close the connection | |
print("Closing connection") | |
conn.close() |
Author
dev-ritik
commented
Jul 4, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment