commit 3c72c8a02c3ea3e13f08f0eba9e32a4bba95432f Author: Shvetsov Nikolai Date: Sun Apr 12 21:52:52 2026 +0300 практика2 готова diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..abd6eb2 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +GITEA_TOKEN="2304515ea0129ec81b855b82e24cb7cfad53de93" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6227bca --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.venv/ +__pycache__/ +*.pyc +*.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..39090e1 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Лабораторная работа 2 + +Работа с TCP, UDP, HTTP и API Gitea. + +## Содержимое +- TCP клиент/сервер +- UDP клиент/сервер +- HTTP через socket и requests +- Работа с API Gitea \ No newline at end of file diff --git a/gitea_read.py b/gitea_read.py new file mode 100644 index 0000000..96d1848 --- /dev/null +++ b/gitea_read.py @@ -0,0 +1,18 @@ +import os +import requests + +TOKEN = os.getenv("GITEA_TOKEN") + +if not TOKEN: + raise ValueError("Нет токена в переменной окружения") + +url = "https://git.vyatsu.ru/api/v1/user" + +headers = { + "Authorization": f"token {TOKEN}" +} + +response = requests.get(url, headers=headers) + +print(response.status_code) +print(response.json()) \ No newline at end of file diff --git a/gitea_write.py b/gitea_write.py new file mode 100644 index 0000000..88c7e94 --- /dev/null +++ b/gitea_write.py @@ -0,0 +1,26 @@ +import os +import requests + +TOKEN = os.getenv("GITEA_TOKEN") + +if not TOKEN: + raise ValueError("Нет токена") + +url = "https://git.vyatsu.ru/api/v1/user/repos" + +headers = { + "Authorization": f"token {TOKEN}", + "Content-Type": "application/json" +} + +data = { + "name": "lab3-created-via-api", + "description": "repo created via API", + "private": False, + "auto_init": True +} + +response = requests.post(url, headers=headers, json=data) + +print(response.status_code) +print(response.text) \ No newline at end of file diff --git a/http_requests_client.py b/http_requests_client.py new file mode 100644 index 0000000..6393f5f --- /dev/null +++ b/http_requests_client.py @@ -0,0 +1,8 @@ +import requests + +response = requests.get("http://vyatsu.ru") + +print("Status:", response.status_code) +print("Headers:", response.headers) +print("Body (первые 500 символов):") +print(response.text[:500]) \ No newline at end of file diff --git a/http_socket_client.py b/http_socket_client.py new file mode 100644 index 0000000..c1527fd --- /dev/null +++ b/http_socket_client.py @@ -0,0 +1,27 @@ +import socket + +HOST = "vyatsu.ru" +PORT = 80 + +request = ( + "GET / HTTP/1.1\r\n" + "Host: vyatsu.ru\r\n" + "Connection: close\r\n" + "\r\n" +) + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect((HOST, PORT)) +client.sendall(request.encode()) + +response = b"" + +while True: + data = client.recv(4096) + if not data: + break + response += data + +client.close() + +print(response.decode(errors="ignore")) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4dfd184 Binary files /dev/null and b/requirements.txt differ diff --git a/tcp_client.py b/tcp_client.py new file mode 100644 index 0000000..7157267 --- /dev/null +++ b/tcp_client.py @@ -0,0 +1,15 @@ +import socket + +HOST = "127.0.0.1" +PORT = 10000 + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect((HOST, PORT)) + +message = input("Введите сообщение: ") +client.sendall(message.encode()) + +data = client.recv(1024) +print("Ответ от сервера:", data.decode()) + +client.close() \ No newline at end of file diff --git a/tcp_server.py b/tcp_server.py new file mode 100644 index 0000000..3f4b5ba --- /dev/null +++ b/tcp_server.py @@ -0,0 +1,33 @@ +import socket + +HOST = "0.0.0.0" +PORT = 10000 + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind((HOST, PORT)) +server.listen(1) + +print(f"TCP сервер запущен на {HOST}:{PORT}") + +while True: + conn, addr = server.accept() + print(f"Подключение от {addr}") + + data = conn.recv(1024) + if not data: + conn.close() + continue + + text = data.decode().strip() + print(f"Получено: {text}") + + response = text.upper().encode() + conn.sendall(response) + + conn.close() + + if text.upper() == "EXIT": + print("Завершение сервера") + break + +server.close() \ No newline at end of file diff --git a/udp_client.py b/udp_client.py new file mode 100644 index 0000000..ca76ef0 --- /dev/null +++ b/udp_client.py @@ -0,0 +1,14 @@ +import socket + +HOST = "127.0.0.1" +PORT = 10001 + +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +message = input("Введите сообщение: ") +client.sendto(message.encode(), (HOST, PORT)) + +data, _ = client.recvfrom(1024) +print("Ответ от сервера:", data.decode()) + +client.close() \ No newline at end of file diff --git a/udp_server.py b/udp_server.py new file mode 100644 index 0000000..dbfb04c --- /dev/null +++ b/udp_server.py @@ -0,0 +1,17 @@ +import socket + +HOST = "0.0.0.0" +PORT = 10001 + +server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +server.bind((HOST, PORT)) + +print(f"UDP сервер запущен на {HOST}:{PORT}") + +while True: + data, addr = server.recvfrom(1024) + text = data.decode().strip() + print(f"Сообщение от {addr}: {text}") + + response = text.upper().encode() + server.sendto(response, addr) \ No newline at end of file