practice03-api-mamaev/http_client_socket.py

30 lines
556 B
Python

import socket
HOST = "vyatsu.ru"
PORT = 80
def main():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
request = (
"GET / HTTP/1.1\r\n"
f"Host: {HOST}\r\n"
"Connection: close\r\n"
"\r\n"
)
client.sendall(request.encode())
response = b""
while True:
chunk = client.recv(4096)
if not chunk:
break
response += chunk
print(response.decode(errors="ignore"))
client.close()
if __name__ == "__main__":
main()