3laba/cod/http_requests_client.py
2026-05-08 11:24:20 +03:00

40 lines
1.3 KiB
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.

# http_requests_client.py
import requests
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
def http_get_requests():
"""HTTP GET запрос через библиотеку requests"""
try:
url = "http://vyatsu.ru"
# Выполняем GET запрос
logging.info(f"Запрос к {url}")
response = requests.get(url, timeout=10)
# Информация об ответе
logging.info(f"Статус код: {response.status_code} {response.reason}")
logging.info(f"Время ответа: {response.elapsed.total_seconds():.3f} сек")
logging.info(f"Размер ответа: {len(response.content)} байт")
# Заголовки ответа
logging.info("Основные заголовки:")
for header in ['content-type', 'server', 'date']:
if header in response.headers:
logging.info(f" {header}: {response.headers[header]}")
# Тело ответа (первые 500 символов)
logging.info(f"Тело ответа:\n{response.text[:500]}")
return response
except requests.exceptions.RequestException as e:
logging.error(f"Ошибка запроса: {e}")
return None
if __name__ == '__main__':
http_get_requests()