Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

11 changed files with 166 additions and 2 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
venv/
.env/
__pycache__/
*.pyc
.vscode/
.idea/
*.log
api_git_example.py
token.py

View File

@ -1,3 +1,32 @@
# lab3-api-repo
\# Учебная практика №2.3 — Работа с сетевыми соединениями в Python
Репозиторий содержит решение ЛР3: примеры работы с сетевыми протоколами TCP и UDP,
HTTP-запросами и анализом трафика в Wireshark, а также примеры обращения к API Gitea
с использованием токенов доступа.
\## Содержание
\- `tcp\_server.py`, `tcp\_client.py` — обмен данными по TCP
\- `udp\_server.py`, `udp\_client.py` — обмен данными по UDP
\- `http\_socket\_client.py` — HTTP-запрос к `vyatsu.ru` через модуль `socket`
\- `requests\_example.py` — HTTP-запрос к `vyatsu.ru` через библиотеку `requests`
\- `gitea\_read\_user.py` — запрос к API Gitea `/api/v1/user` с токеном только на чтение
\- `gitea\_create\_repo.py` — пример обращения к API Gitea с токеном на запись
\- `.gitignore` — файлы и каталоги, которые не добавляются в репозиторий
\- `README.md` — краткое описание работы
Репозиторий создан через Gitea API

6
api_git_example.py Normal file
View File

@ -0,0 +1,6 @@
import requests
TOKEN = "GIT_VYATSU_TOKEN1"
headers = {"Authorization": f"token {TOKEN}"}
response = requests.get("https://git.vyatsu.ru/api/v1/user", headers=headers)
print(response.json())

19
gitea_create_repo.py Normal file
View File

@ -0,0 +1,19 @@
import os
import requests
TOKEN = os.getenv("GITEA_WRITE_TOKEN")
headers = {
"Authorization": f"token {TOKEN}",
"Content-Type": "application/json"
}
data = {
"name": "lab3-api-repo",
"description": "Репозиторий создан через Gitea API",
"private": False,
"auto_init": True
}
response = requests.post("https://git.vyatsu.ru/api/v1/user/repos", json=data, headers=headers)
print(response.status_code)
print(response.json())

13
gitea_read_user.py Normal file
View File

@ -0,0 +1,13 @@
import os
import requests
TOKEN = os.getenv("GITEA_READ_TOKEN")
if not TOKEN:
raise RuntimeError("Переменная окружения GITEA_READ_TOKEN не задана")
headers = {"Authorization": f"token {TOKEN}"}
response = requests.get("https://git.vyatsu.ru/api/v1/user", headers=headers)
print(response.status_code)
print(response.text)

17
http_socket_client.py Normal file
View File

@ -0,0 +1,17 @@
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("vyatsu.ru", 80))
request = (
"GET / HTTP/1.1\r\n"
"Host: vyatsu.ru\r\n"
"Connection: close\r\n"
"\r\n"
)
client.sendall(request.encode("utf-8"))
response = client.recv(4096)
print(response.decode(errors="ignore"))
client.close()

4
requests_example.py Normal file
View File

@ -0,0 +1,4 @@
import requests
response = requests.get("http://vyatsu.ru")
print(response.text[:500])

12
tcp_client.py Normal file
View File

@ -0,0 +1,12 @@
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 12000))
message = "hello server"
client.sendall(message.encode())
data = client.recv(1024)
print(f"Ответ от сервера: {data.decode()}")
client.close()

30
tcp_server.py Normal file
View File

@ -0,0 +1,30 @@
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 12000))
server.listen(1)
print("TCP сервер запущен и ожидает подключения...")
while True:
conn, addr = server.accept()
print(f"Подключение от {addr}")
data = conn.recv(1024)
if not data:
conn.close()
continue
message = data.decode()
print(f"Получено сообщение: {message}")
response = message.upper()
conn.sendall(response.encode())
conn.close()
if response == "EXIT":
print("Сервер завершает работу")
break
server.close()

11
udp_client.py Normal file
View File

@ -0,0 +1,11 @@
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = "привет сервер"
client.sendto(message.encode(), ("127.0.0.1", 10001))
data, _ = client.recvfrom(1024)
print(f"Ответ от сервера: {data.decode()}")
client.close()

14
udp_server.py Normal file
View File

@ -0,0 +1,14 @@
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(("0.0.0.0", 10001))
print("UDP сервер запущен и ожидает сообщения...")
while True:
data, addr = server.recvfrom(1024)
message = data.decode()
print(f"Сообщение от {addr}: {message}")
response = message.upper()
server.sendto(response.encode(), addr)