Skip to content

Instantly share code, notes, and snippets.

@FrenchTechLead
Last active December 15, 2020 22:07
Show Gist options
  • Save FrenchTechLead/0525a0784e255b9b57be56644aa0a985 to your computer and use it in GitHub Desktop.
Save FrenchTechLead/0525a0784e255b9b57be56644aa0a985 to your computer and use it in GitHub Desktop.
Simple http client using python sockets.
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Establishing the TCP connection between the python client and the server
# which is listening on port 80.
client.connect(("o1o.herokuapp.com", 80))
# Constructing the HTTP GET request.
request_line_1 = "GET /posts/post-1.html HTTP/1.1\r\n"
request_line_2 = "Host:o1o.herokuapp.com\r\n"
request_line_3 = "\r\n"
request_string = request_line_1 + request_line_2 + request_line_3
print("===================HTTP Request string===============================")
print(request_string)
print("===================HTTP Request bytes===============================")
request_bytes = request_string.encode("utf-8")
print(request_bytes)
# Sending the request bytes.
client.send(request_bytes)
# Receiving the response bytes.
response_bytes = client.recv(500)
print("===================HTTP Response bytes===============================")
print(response_bytes)
# Decoding the request bytes.
response_str = response_bytes.decode("utf-8")
print("===================HTTP Response string==============================")
print(response_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment