lab3/HTTP_request.py

31 lines
782 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import socket
import ssl
# Создаём соединение
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
secure_client = ssl.create_default_context().wrap_socket(
client, server_hostname='vyatsu.ru'
)
secure_client.connect(('vyatsu.ru', 443))
# HTTP-запрос с заголовками
request = (
"GET / HTTP/1.1\r\n"
"Host: vyatsu.ru\r\n"
"User-Agent: python-requests/2.28.1\r\n"
"Accept: */*\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Connection: keep-alive\r\n"
"\r\n"
)
secure_client.sendall(request.encode())
# Получаем и распаковываем ответ
response = secure_client.recv(8192)
headers, body = response.split(b'\r\n\r\n', 1)
print(headers.decode())
print(body.decode())
secure_client.close()