Files
lab3/http_socket_matched.py
T

27 lines
815 B
Python

"""Send the same HTTP headers as requests over a raw TCP connection."""
import socket
from http.client import HTTPResponse
from requests.utils import default_headers
def main() -> None:
host = "vyatsu.ru"
headers = {"Host": host, **default_headers(), "Connection": "close"}
lines = ["GET / HTTP/1.1", *(f"{name}: {value}" for name, value in headers.items())]
request = "\r\n".join([*lines, "", ""])
with socket.create_connection((host, 80), timeout=10) as client:
client.sendall(request.encode("ascii"))
response = HTTPResponse(client)
try:
response.begin()
print(f"HTTP/1.1 {response.status} {response.reason}")
print(response.headers, end="")
finally:
response.close()
if __name__ == "__main__":
main()