Add TCP and UDP clients and servers
This commit is contained in:
parent
5938e80bc8
commit
820aa0e123
8
clients/tcp_client.py
Normal file
8
clients/tcp_client.py
Normal file
@ -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()
|
||||
7
clients/udp_client.py
Normal file
7
clients/udp_client.py
Normal file
@ -0,0 +1,7 @@
|
||||
import socket
|
||||
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
client.sendto(b'hello server', ('127.0.0.1', 20221))
|
||||
data, _ = client.recvfrom(1024)
|
||||
print(f"Ответ от сервера: {data.decode()}")
|
||||
client.close()
|
||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
requests
|
||||
21
servers/tcp_server.py
Normal file
21
servers/tcp_server.py
Normal file
@ -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
|
||||
12
servers/udp_server.py
Normal file
12
servers/udp_server.py
Normal file
@ -0,0 +1,12 @@
|
||||
import socket
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
server.bind(('0.0.0.0', 20221))
|
||||
print("UDP сервер запущен")
|
||||
|
||||
while True:
|
||||
data, addr = server.recvfrom(1024)
|
||||
date_original = data.decode()
|
||||
print(f"Сообщение от {addr}: {date_original}")
|
||||
modif = "[SERVER] " + date_original[::-1].upper()
|
||||
server.sendto(modif.encode(), addr)
|
||||
Loading…
Reference in New Issue
Block a user