feat: add HTTP requests and socket clients
This commit is contained in:
1 parent
48b3cca2c4
commit
1b8429e76e
2 files changed
+39
No files matched your search
@@ -0,0 +1,7 @@
|
||||
import requests
|
||||
|
||||
response = requests.get("http://vyatsu.ru")
|
||||
print("Итоговый URL после редиректа:", response.url)
|
||||
print("Статус ответа:", response.status_code)
|
||||
print("Фрагмент HTML-страницы:\n")
|
||||
print(response.text[:500])
|
||||
@@ -0,0 +1,32 @@
|
||||
import socket
|
||||
import ssl
|
||||
|
||||
host = 'vyatsu.ru'
|
||||
port = 443
|
||||
|
||||
# Оборачиваем TCP-сокет в SSL/TLS, так как vyatsu.ru перенаправляет HTTP (80) на HTTPS (443)
|
||||
context = ssl.create_default_context()
|
||||
raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
with context.wrap_socket(raw_sock, server_hostname=host) as client:
|
||||
client.connect((host, port))
|
||||
|
||||
# Заголовки, аналогичные библиотеке requests
|
||||
request = (
|
||||
f"GET / HTTP/1.1\r\n"
|
||||
f"Host: {host}\r\n"
|
||||
f"User-Agent: python-requests/2.32.0\r\n"
|
||||
f"Accept-Encoding: identity\r\n"
|
||||
f"Accept: */*\r\n"
|
||||
f"Connection: close\r\n\r\n"
|
||||
)
|
||||
client.sendall(request.encode('utf-8'))
|
||||
|
||||
response = b""
|
||||
while True:
|
||||
chunk = client.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
response += chunk
|
||||
|
||||
print(response.decode('utf-8', errors='ignore')[:700])
|
||||
Reference in new issue
Block a user