Skip to content

Instantly share code, notes, and snippets.

@dev-ritik
Created May 10, 2020 15:06
Show Gist options
  • Save dev-ritik/99609f5282a7cad5d13f621cd80431fe to your computer and use it in GitHub Desktop.
Save dev-ritik/99609f5282a7cad5d13f621cd80431fe to your computer and use it in GitHub Desktop.
Simple HTTPS client example
#!/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()
@dev-ritik
Copy link
Author

⚠️ DO NOT DELETE ⚠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment