практика2 готова
This commit is contained in:
commit
3c72c8a02c
1
.env.example
Normal file
1
.env.example
Normal file
@ -0,0 +1 @@
|
||||
GITEA_TOKEN="2304515ea0129ec81b855b82e24cb7cfad53de93"
|
||||
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.venv/
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.env
|
||||
9
README.md
Normal file
9
README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Лабораторная работа 2
|
||||
|
||||
Работа с TCP, UDP, HTTP и API Gitea.
|
||||
|
||||
## Содержимое
|
||||
- TCP клиент/сервер
|
||||
- UDP клиент/сервер
|
||||
- HTTP через socket и requests
|
||||
- Работа с API Gitea
|
||||
18
gitea_read.py
Normal file
18
gitea_read.py
Normal file
@ -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())
|
||||
26
gitea_write.py
Normal file
26
gitea_write.py
Normal file
@ -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)
|
||||
8
http_requests_client.py
Normal file
8
http_requests_client.py
Normal file
@ -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])
|
||||
27
http_socket_client.py
Normal file
27
http_socket_client.py
Normal file
@ -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"))
|
||||
BIN
requirements.txt
Normal file
BIN
requirements.txt
Normal file
Binary file not shown.
15
tcp_client.py
Normal file
15
tcp_client.py
Normal file
@ -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()
|
||||
33
tcp_server.py
Normal file
33
tcp_server.py
Normal file
@ -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()
|
||||
14
udp_client.py
Normal file
14
udp_client.py
Normal file
@ -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()
|
||||
17
udp_server.py
Normal file
17
udp_server.py
Normal file
@ -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)
|
||||
Loading…
Reference in New Issue
Block a user