tsest_rep/http_socket.py

64 lines
2.2 KiB
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
def http_via_socket():
# Создаем TCP сокет
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print("🔄 Устанавливаем соединение с vyatsu.ru...")
client.connect(('vyatsu.ru', 80))
print("✅ Соединение установлено")
# Формируем HTTP-запрос
request = (
"GET / HTTP/1.1\r\n"
"Host: vyatsu.ru\r\n"
"User-Agent: Python-Socket-Client/1.0\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7\r\n"
"Connection: close\r\n"
"\r\n"
)
print("📤 Отправляем HTTP-запрос...")
client.sendall(request.encode())
print("📨 Получаем ответ...")
response = b""
while True:
chunk = client.recv(4096)
if not chunk:
break
response += chunk
# Декодируем ответ
decoded_response = response.decode('utf-8', errors='ignore')
print("\n" + "="*50)
print("ОТВЕТ ОТ СЕРВЕРА:")
print("="*50)
# Отделяем заголовки от тела
headers_end = decoded_response.find('\r\n\r\n')
if headers_end != -1:
headers = decoded_response[:headers_end]
body_start = headers_end + 4
body_preview = decoded_response[body_start:body_start + 500]
print("📋 ЗАГОЛОВКИ:")
print(headers)
print(f"\n📄 ТЕЛО (первые 500 символов):")
print(body_preview)
if len(decoded_response) > body_start + 500:
print("... (ответ обрезан)")
else:
print(decoded_response[:1000])
except Exception as e:
print(f"❌ Ошибка: {e}")
finally:
client.close()
print("\n🔒 Соединение закрыто")
if __name__ == "__main__":
http_via_socket()