diff --git a/.gitignore b/.gitignore index e69de29..9ad5c37 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +config.py \ No newline at end of file diff --git a/README.md b/README.md index e69de29..2136508 100644 --- a/README.md +++ b/README.md @@ -0,0 +1 @@ +# Практика: Сетевые соединения в Python \ No newline at end of file diff --git a/__pycache__/config.cpython-314.pyc b/__pycache__/config.cpython-314.pyc new file mode 100644 index 0000000..6b92679 Binary files /dev/null and b/__pycache__/config.cpython-314.pyc differ diff --git a/gitea_read.py b/gitea_read.py new file mode 100644 index 0000000..7d56126 --- /dev/null +++ b/gitea_read.py @@ -0,0 +1,7 @@ +import requests +from config import TOKEN + +headers = {"Authorization": f"token {TOKEN}"} +response = requests.get("https://git.vyatsu.ru/api/v1/user", headers=headers) + +print(response.json()) \ No newline at end of file diff --git a/http_requests.py b/http_requests.py new file mode 100644 index 0000000..83588ae --- /dev/null +++ b/http_requests.py @@ -0,0 +1,4 @@ +import requests + +response = requests.get("http://vyatsu.ru") +print(response.text[:500]) \ No newline at end of file diff --git a/http_socket.py b/http_socket.py new file mode 100644 index 0000000..4c1fa7e --- /dev/null +++ b/http_socket.py @@ -0,0 +1,9 @@ +import socket + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect(('vyatsu.ru', 80)) +request = "GET / HTTP/1.1\r\nHost: vyatsu.ru\r\n\r\n" +client.sendall(request.encode()) +response = client.recv(4096) +print(response.decode()) +client.close() \ No newline at end of file diff --git a/tcp_client.py b/tcp_client.py new file mode 100644 index 0000000..759be70 --- /dev/null +++ b/tcp_client.py @@ -0,0 +1,8 @@ +import socket + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect(('127.0.0.1', 10000)) +client.sendall(b'EXIT') +data = client.recv(1024) +print(f"Ответ от сервера: {data.decode()}") +client.close() \ No newline at end of file diff --git a/tcp_server.py b/tcp_server.py new file mode 100644 index 0000000..8f14be6 --- /dev/null +++ b/tcp_server.py @@ -0,0 +1,21 @@ +import socket + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind(("0.0.0.0", 10000)) +server.listen(1) +print("TCP сервер запущен") + +while True: + + conn, addr = server.accept() + print(f"Подключение от {addr}") + + data = conn.recv(1024) + if not data: + break + conn.sendall(data.upper()) + + conn.close() + + if data.upper() == b'EXIT': + break \ No newline at end of file diff --git a/udp_client.py b/udp_client.py new file mode 100644 index 0000000..3a98998 --- /dev/null +++ b/udp_client.py @@ -0,0 +1,7 @@ +import socket + +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +client.sendto(b'hello server', ('127.0.0.1', 10005)) +data, _ = client.recvfrom(1024) +print(f"Ответ от сервера: {data.decode()}") +client.close() \ No newline at end of file diff --git a/udp_server.py b/udp_server.py new file mode 100644 index 0000000..eeb40f8 --- /dev/null +++ b/udp_server.py @@ -0,0 +1,10 @@ +import socket + +server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +server.bind(('0.0.0.0', 10005)) +print("UDP сервер запущен") + +while True: + data, addr = server.recvfrom(1024) + print(f"Сообщение от {addr}: {data.decode()}") + server.sendto(b"MODIFIED: " + data, addr) \ No newline at end of file