task3/socket_client.py

25 lines
699 B
Python
Raw Permalink 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 requests
# Используем requests для получения данных
response = requests.get('http://vyatsu.ru')
print("Ответ с использованием requests:")
print(response.text)
# Ручной запрос через сокет (TCP)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('vyatsu.ru', 80))
request = "GET / HTTP/1.1\r\nHost: vyatsu.ru\r\nConnection: close\r\n\r\n"
client.sendall(request.encode())
response = b""
while True:
part = client.recv(4096)
if not part:
break
response += part
print("\nОтвет с использованием socket:")
print(response.decode())
client.close()