commit 18c98210fd6862e18e00d723e722c50de47feb31 Author: Шавлович Маргарита Date: Thu Apr 16 23:02:46 2026 +0300 Третья работа: TCP, UDP, HTTP, Gitea API diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f0d76c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.venv/ +__pycache__/ +*.pyc +.env \ No newline at end of file diff --git a/client_tcp.py b/client_tcp.py new file mode 100644 index 0000000..484b563 --- /dev/null +++ b/client_tcp.py @@ -0,0 +1,15 @@ +import socket + +HOST = "127.0.0.1" +PORT = 12345 + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect((HOST, PORT)) + +message = input("Введите сообщение: ") +client.sendall(message.encode("utf-8")) + +response = client.recv(1024) +print("Ответ от сервера:", response.decode("utf-8")) + +client.close() \ No newline at end of file diff --git a/client_udp.py b/client_udp.py new file mode 100644 index 0000000..c83cf95 --- /dev/null +++ b/client_udp.py @@ -0,0 +1,14 @@ +import socket + +HOST = "127.0.0.1" +PORT = 12346 + +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +message = input("Введите сообщение: ") +client.sendto(message.encode("utf-8"), (HOST, PORT)) + +response, addr = client.recvfrom(1024) +print("Ответ от сервера:", response.decode("utf-8")) + +client.close() \ No newline at end of file diff --git a/gitea_create_comment.py b/gitea_create_comment.py new file mode 100644 index 0000000..aba6c10 --- /dev/null +++ b/gitea_create_comment.py @@ -0,0 +1,30 @@ +import os +import requests +from dotenv import load_dotenv + +load_dotenv() + +TOKEN = os.getenv("GITEA_WRITE_TOKEN") +BASE_URL = os.getenv("GITEA_BASE_URL", "https://git.vyatsu.ru") + +OWNER = "stud203994" +REPO = "test" +ISSUE_INDEX = 1 + +headers = { + "Authorization": f"token {TOKEN}", + "Content-Type": "application/json" +} + +data = { + "body": "Комментарий добавлен через API Gitea." +} + +response = requests.post( + f"{BASE_URL}/api/v1/repos/{OWNER}/{REPO}/issues/{ISSUE_INDEX}/comments", + headers=headers, + json=data +) + +print("Статус:", response.status_code) +print(response.json()) \ No newline at end of file diff --git a/gitea_create_issue.py b/gitea_create_issue.py new file mode 100644 index 0000000..64ca1d9 --- /dev/null +++ b/gitea_create_issue.py @@ -0,0 +1,30 @@ +import os +import requests +from dotenv import load_dotenv + +load_dotenv() + +TOKEN = os.getenv("GITEA_WRITE_TOKEN") +BASE_URL = os.getenv("GITEA_BASE_URL", "https://git.vyatsu.ru") + +OWNER = "stud203994" +REPO = "test" + +headers = { + "Authorization": f"token {TOKEN}", + "Content-Type": "application/json" +} + +data = { + "title": "Тестовый issue", + "body": "Issue создан через API Gitea из Python." +} + +response = requests.post( + f"{BASE_URL}/api/v1/repos/{OWNER}/{REPO}/issues", + headers=headers, + json=data +) + +print("Статус:", response.status_code) +print(response.json()) \ No newline at end of file diff --git a/gitea_create_repo.py b/gitea_create_repo.py new file mode 100644 index 0000000..d851410 --- /dev/null +++ b/gitea_create_repo.py @@ -0,0 +1,25 @@ +import os +import requests +from dotenv import load_dotenv + +load_dotenv() + +TOKEN = os.getenv("GITEA_WRITE_TOKEN") +BASE_URL = os.getenv("GITEA_BASE_URL", "https://git.vyatsu.ru") + +headers = { + "Authorization": f"token {TOKEN}", + "Content-Type": "application/json" +} + +data = { + "name": "third-work-network-python", + "description": "Третья работа по сетевым соединениям в Python", + "private": False, + "auto_init": True +} + +response = requests.post(f"{BASE_URL}/api/v1/user/repos", headers=headers, json=data) + +print("Статус:", response.status_code) +print(response.json()) \ No newline at end of file diff --git a/gitea_user_read.py b/gitea_user_read.py new file mode 100644 index 0000000..3c007ef --- /dev/null +++ b/gitea_user_read.py @@ -0,0 +1,17 @@ +import os +import requests +from dotenv import load_dotenv + +load_dotenv() + +TOKEN = os.getenv("GITEA_READ_TOKEN") +BASE_URL = os.getenv("GITEA_BASE_URL", "https://git.vyatsu.ru") + +headers = { + "Authorization": f"token {TOKEN}" +} + +response = requests.get(f"{BASE_URL}/api/v1/user", headers=headers) + +print("Статус:", response.status_code) +print(response.json()) \ No newline at end of file diff --git a/http_requests_client.py b/http_requests_client.py new file mode 100644 index 0000000..67f231c --- /dev/null +++ b/http_requests_client.py @@ -0,0 +1,7 @@ +import requests + +response = requests.get("http://vyatsu.ru") + +print("Статус-код:", response.status_code) +print("Первые 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..99ec300 --- /dev/null +++ b/http_socket_client.py @@ -0,0 +1,29 @@ +import socket + +HOST = "vyatsu.ru" +PORT = 80 + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect((HOST, PORT)) + +request = ( + "GET / HTTP/1.1\r\n" + "Host: vyatsu.ru\r\n" + "Connection: close\r\n" + "User-Agent: PythonSocketClient/1.0\r\n" + "Accept: */*\r\n" + "\r\n" +) + +client.sendall(request.encode("utf-8")) + +response = b"" +while True: + part = client.recv(4096) + if not part: + break + response += part + +client.close() + +print(response.decode("utf-8", errors="ignore")) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2e836bd --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +библиотеки которые использовались requests \ No newline at end of file diff --git a/server_tcp.py b/server_tcp.py new file mode 100644 index 0000000..d2fd2a1 --- /dev/null +++ b/server_tcp.py @@ -0,0 +1,25 @@ +import socket + +HOST = "127.0.0.1" +PORT = 12345 + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind((HOST, PORT)) +server.listen(1) + +print("TCP сервер запущен") + +conn, addr = server.accept() +print(f"Подключение от {addr}") + +data = conn.recv(1024) + +if data: + text = data.decode("utf-8") + print("Получено от клиента:", text) + + response = "Ответ сервера: " + text.upper() + conn.sendall(response.encode("utf-8")) + +conn.close() +server.close() \ No newline at end of file diff --git a/server_udp.py b/server_udp.py new file mode 100644 index 0000000..f89fac5 --- /dev/null +++ b/server_udp.py @@ -0,0 +1,19 @@ +import socket + +HOST = "127.0.0.1" +PORT = 12346 + +server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +server.bind((HOST, PORT)) + +print("UDP сервер запущен") + +data, addr = server.recvfrom(1024) +text = data.decode("utf-8") + +print(f"Получено от {addr}: {text}") + +response = "Ответ сервера: " + text.upper() +server.sendto(response.encode("utf-8"), addr) + +server.close() \ No newline at end of file