HTTP: socket and requests

This commit is contained in:
parent 28ed52b969
commit b548501d1a
2 changed files with 30 additions and 0 deletions

4
HTTP/requests_client.py Normal file
View File

@ -0,0 +1,4 @@
import requests
response = requests.get("http://vyatsu.ru")
print(response.text[:500])

26
HTTP/socket_client.py Normal file
View File

@ -0,0 +1,26 @@
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('vyatsu.ru', 80))
request = (
"GET / HTTP/1.1\r\n"
"Host: vyatsu.ru\r\n"
"User-Agent: python-requests/2.33.1\r\n"
"Accept-Encoding: gzip, deflate, zstd\r\n" # ← Закомментировали
"Accept: */*\r\n"
"Connection: close\r\n" # ← лучше keep-alive заменить на close
"\r\n"
)
client.sendall(request.encode())
full_response = b""
while True:
chunk = client.recv(4096)
if not chunk:
break
full_response += chunk
print(full_response.decode())
client.close()