You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
10 months ago
|
import socket
|
||
|
import threading
|
||
|
|
||
|
def handle_client(client_socket):
|
||
|
|
||
|
request = ""
|
||
|
response = "test"
|
||
|
while True:
|
||
|
data = client_socket.recv(1).decode('UTF-8')
|
||
|
if data == '1':
|
||
|
response = "1. Hello"
|
||
|
print('1')
|
||
|
break
|
||
|
elif data == '2':
|
||
|
response = "2. Hola"
|
||
|
print('2')
|
||
|
break
|
||
|
elif data == '3':
|
||
|
response = "3. Bonjour"
|
||
|
print('3')
|
||
|
break
|
||
|
elif data == '\n':
|
||
|
response = request.upper()
|
||
|
break
|
||
|
request += data
|
||
|
|
||
|
print(request.upper())
|
||
|
|
||
|
client_socket.send(response.encode('UTF-8'))
|
||
|
|
||
|
client_socket.close()
|
||
|
|
||
|
def start_server():
|
||
|
host = ''
|
||
|
port = 3333
|
||
|
|
||
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
server_socket.bind((host, port))
|
||
|
server_socket.listen(5)
|
||
|
print(f"Server listening on {host}:{port}")
|
||
|
|
||
|
while True:
|
||
|
client_socket, addr = server_socket.accept()
|
||
|
print(f"Accepted connection from {addr[0]}:{addr[1]}")
|
||
|
|
||
|
client_handler = threading.Thread(target=handle_client, args=(client_socket,))
|
||
|
client_handler.start()
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
start_server()
|