1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
import socket
def get_raw_http_response(host, port, path):
CRLF = b"\r\n"
request = [
b"GET " + path.encode("ascii") + b" HTTP/1.1",
b"Host: " + host.encode("ascii"),
b"Connection: Close",
b"",
b"",
]
# Connect to the server
with socket.socket() as s:
s.connect((host, port))
# Send an HTTP request
s.send(CRLF.join(request))
# Get the response (in several parts, if necessary)
response = b""
buffer = s.recv(4096)
while buffer:
response += buffer
buffer = s.recv(4096)
return response
|