Add TCP/UDP server-client applications
This commit is contained in:
parent
63bfdd0759
commit
dca62325b0
18
03_networking/gitea_api.py
Normal file
18
03_networking/gitea_api.py
Normal file
@ -0,0 +1,18 @@
|
||||
import requests
|
||||
|
||||
GITEA_URL = "https://git.vyatsu.ru" # Замените на ваш URL
|
||||
TOKEN = "02bffb2de3b71f8c3132dad0d22ea2de8d4b6333" # Сгенерировать в Settings -> Applications
|
||||
HEADERS = {"Authorization": f"token {TOKEN}", "Accept": "application/json"}
|
||||
|
||||
# 1. Информация о пользователе
|
||||
me = requests.get(f"{GITEA_URL}/api/v1/user", headers=HEADERS)
|
||||
print(f"👤 User: {me.json()['login']}")
|
||||
|
||||
# 2. Создание Issue
|
||||
payload = {
|
||||
"title": "Тестовый Issue из практики",
|
||||
"body": "Создано автоматически через API Python."
|
||||
}
|
||||
repo = "stud182932/main" # Замените на owner/repo
|
||||
res = requests.post(f"{GITEA_URL}/api/v1/repos/{repo}/issues", json=payload, headers=HEADERS)
|
||||
print(f"✅ Issue создан: {res.json().get('html_url', res.text)}")
|
||||
21
03_networking/http_raw.py
Normal file
21
03_networking/http_raw.py
Normal file
@ -0,0 +1,21 @@
|
||||
import socket
|
||||
|
||||
HOST, PORT = "vyatsu.ru", 80
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.settimeout(10)
|
||||
s.connect((HOST, PORT))
|
||||
|
||||
# Формируем сырой HTTP-запрос
|
||||
request = "GET / HTTP/1.1\r\nHost: vyatsu.ru\r\nConnection: close\r\n\r\n"
|
||||
s.sendall(request.encode('utf-8'))
|
||||
|
||||
response = b""
|
||||
while True:
|
||||
chunk = s.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
response += chunk
|
||||
|
||||
print("📡 Сырой ответ сервера (первые 800 символов):")
|
||||
print(response.decode('utf-8', errors='ignore')[:800])
|
||||
23
03_networking/tcp_client.py
Normal file
23
03_networking/tcp_client.py
Normal file
@ -0,0 +1,23 @@
|
||||
import socket
|
||||
|
||||
def main():
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 65432
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.connect((HOST, PORT))
|
||||
print("Подключено к серверу")
|
||||
|
||||
messages = ["Привет!", "Как дела?", "exit"]
|
||||
for msg in messages:
|
||||
print(f"Отправка: {msg}")
|
||||
s.sendall(msg.encode('utf-8'))
|
||||
|
||||
if msg.lower() == 'exit':
|
||||
break
|
||||
|
||||
response = s.recv(1024)
|
||||
print(f"Ответ: {response.decode()}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
32
03_networking/tcp_server.py
Normal file
32
03_networking/tcp_server.py
Normal file
@ -0,0 +1,32 @@
|
||||
import socket
|
||||
|
||||
def main():
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 65432
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind((HOST, PORT))
|
||||
s.listen(1)
|
||||
print(f"TCP сервер запущен на {HOST}:{PORT}")
|
||||
|
||||
conn, addr = s.accept()
|
||||
with conn:
|
||||
print(f"Подключен клиент: {addr}")
|
||||
while True:
|
||||
data = conn.recv(1024)
|
||||
if not data:
|
||||
break
|
||||
|
||||
message = data.decode('utf-8').strip()
|
||||
print(f"Получено: {message}")
|
||||
|
||||
if message.lower() == 'exit':
|
||||
print("Завершение работы...")
|
||||
break
|
||||
|
||||
response = f"SERVER: {message.upper()}"
|
||||
conn.sendall(response.encode('utf-8'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
20
03_networking/udp_client.py
Normal file
20
03_networking/udp_client.py
Normal file
@ -0,0 +1,20 @@
|
||||
import socket
|
||||
|
||||
def main():
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 10001
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
messages = ["Привет UDP!", "Тест", "exit"]
|
||||
for msg in messages:
|
||||
print(f"Отправка: {msg}")
|
||||
s.sendto(msg.encode('utf-8'), (HOST, PORT))
|
||||
|
||||
if msg.lower() == 'exit':
|
||||
break
|
||||
|
||||
data, _ = s.recvfrom(1024)
|
||||
print(f"Ответ: {data.decode()}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
23
03_networking/udp_server.py
Normal file
23
03_networking/udp_server.py
Normal file
@ -0,0 +1,23 @@
|
||||
import socket
|
||||
|
||||
def main():
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 10001
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.bind((HOST, PORT))
|
||||
print(f"UDP сервер запущен на {HOST}:{PORT}")
|
||||
|
||||
while True:
|
||||
data, addr = s.recvfrom(1024)
|
||||
message = data.decode('utf-8').strip()
|
||||
print(f"От {addr}: {message}")
|
||||
|
||||
if message.lower() == 'exit':
|
||||
break
|
||||
|
||||
response = f"UDP: {message.upper()}"
|
||||
s.sendto(response.encode('utf-8'), addr)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Reference in New Issue
Block a user