Вторая работа: TCP, UDP, HTTP, Gitea API
This commit is contained in:
commit
1cf83e9af7
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.venv/
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.env
|
||||||
15
client_tcp.py
Normal file
15
client_tcp.py
Normal file
@ -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()
|
||||||
14
client_udp.py
Normal file
14
client_udp.py
Normal file
@ -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()
|
||||||
30
gitea_create_comment.py
Normal file
30
gitea_create_comment.py
Normal file
@ -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())
|
||||||
30
gitea_create_issue.py
Normal file
30
gitea_create_issue.py
Normal file
@ -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())
|
||||||
25
gitea_create_repo.py
Normal file
25
gitea_create_repo.py
Normal file
@ -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())
|
||||||
17
gitea_user_read.py
Normal file
17
gitea_user_read.py
Normal file
@ -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())
|
||||||
7
http_requests_client.py
Normal file
7
http_requests_client.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
response = requests.get("http://vyatsu.ru")
|
||||||
|
|
||||||
|
print("Статус-код:", response.status_code)
|
||||||
|
print("Первые 500 символов ответа:")
|
||||||
|
print(response.text[:500])
|
||||||
29
http_socket_client.py
Normal file
29
http_socket_client.py
Normal file
@ -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"))
|
||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
библиотеки которые использовались requests
|
||||||
25
server_tcp.py
Normal file
25
server_tcp.py
Normal file
@ -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()
|
||||||
19
server_udp.py
Normal file
19
server_udp.py
Normal file
@ -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()
|
||||||
Loading…
Reference in New Issue
Block a user