From 44396aa39570634730473ef9d433948146696d42 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 18 May 2025 19:18:07 +0300 Subject: [PATCH] Add lab6 --- .../lab6/task1/client.py" | 11 +++++ .../lab6/task1/server.py" | 15 +++++++ .../lab6/task2/client.py" | 17 ++++++++ .../lab6/task2/server.py" | 18 +++++++++ .../lab6/task3/index.html" | 9 +++++ .../lab6/task3/server.py" | 23 +++++++++++ .../lab6/task4/client.py" | 23 +++++++++++ .../lab6/task4/server.py" | 40 +++++++++++++++++++ 8 files changed, 156 insertions(+) create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/client.py" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/server.py" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/client.py" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/server.py" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/index.html" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/server.py" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/client.py" create mode 100644 "work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/server.py" diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/client.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/client.py" new file mode 100644 index 000000000..1881ce7a4 --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/client.py" @@ -0,0 +1,11 @@ +import socket + +HOST = 'localhost' +PORT = 4321 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((HOST, PORT)) + s.sendall(b'Hello, server') + data = s.recv(1024) + +print('Received from server:', data.decode()) \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/server.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/server.py" new file mode 100644 index 000000000..48c6de8dd --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task1/server.py" @@ -0,0 +1,15 @@ +import socket + +HOST = 'localhost' +PORT = 4321 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) + s.listen() + print('Server is listening...') + conn, addr = s.accept() + with conn: + print('Connected by', addr) + data = conn.recv(1024) + print('Received from client:', data.decode()) + conn.sendall(b'Hello, client') \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/client.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/client.py" new file mode 100644 index 000000000..34d87e8d2 --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/client.py" @@ -0,0 +1,17 @@ +import socket + +HOST = 'localhost' +PORT = 23456 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((HOST, PORT)) + print("Choose operation:") + print("1 - Pythagorean theorem") + choice = input("Enter choice: ") + + if choice == '1': + a = input("Enter a: ") + b = input("Enter b: ") + s.sendall(f"{choice} {a} {b}".encode()) + result = s.recv(1024).decode() + print("Result:", result) \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/server.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/server.py" new file mode 100644 index 000000000..183322fed --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task2/server.py" @@ -0,0 +1,18 @@ +import socket +import math + +HOST = 'localhost' +PORT = 23456 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) + s.listen() + print('Server listening for math operations...') + conn, addr = s.accept() + with conn: + print('Connected by', addr) + data = conn.recv(1024).decode().split() + choice, a, b = data + if choice == '1': + result = math.sqrt(float(a)**2 + float(b)**2) + conn.sendall(str(result).encode()) \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/index.html" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/index.html" new file mode 100644 index 000000000..e01b539f3 --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/index.html" @@ -0,0 +1,9 @@ + + + + Simple HTTP Server + + +

Hello from Python HTTP Server!

+ + \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/server.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/server.py" new file mode 100644 index 000000000..de7adca24 --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task3/server.py" @@ -0,0 +1,23 @@ +import socket + +HOST = 'localhost' +PORT = 8080 + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) + s.listen(1) + print('HTTP Server running...') + conn, addr = s.accept() + with conn: + request = conn.recv(1024) + print('Request received:') + print(request.decode()) + + with open("index.html", "r") as f: + response_body = f.read() + + response = 'HTTP/1.1 200 OK\r\n' + response += 'Content-Type: text/html\r\n\r\n' + response += response_body + + conn.sendall(response.encode()) \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/client.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/client.py" new file mode 100644 index 000000000..a6c326ba8 --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/client.py" @@ -0,0 +1,23 @@ +import socket +import threading + +def receive_messages(sock): + while True: + try: + msg = sock.recv(1024).decode() + print(msg) + except: + break + +HOST = 'localhost' +PORT = 56789 + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect((HOST, PORT)) + +thread = threading.Thread(target=receive_messages, args=(client,)) +thread.start() + +while True: + msg = input() + client.send(msg.encode()) \ No newline at end of file diff --git "a/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/server.py" "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/server.py" new file mode 100644 index 000000000..9cc13d296 --- /dev/null +++ "b/work/K3320/\320\241\320\272\320\262\320\276\321\200\321\206\320\276\320\262_\320\230\320\262\320\260\320\275/lab6/task4/server.py" @@ -0,0 +1,40 @@ +import socket +import threading + +clients = [] + +def broadcast(message, conn): + for client in clients: + if client != conn: + try: + client.send(message) + except: + client.close() + clients.remove(client) + +def handle_client(conn, addr): + print(f"New connection from {addr}") + clients.append(conn) + while True: + try: + msg = conn.recv(1024) + if not msg: + break + broadcast(msg, conn) + except: + clients.remove(conn) + conn.close() + break + +HOST = 'localhost' +PORT = 56789 + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind((HOST, PORT)) +server.listen() +print("Chat server started...") + +while True: + conn, addr = server.accept() + thread = threading.Thread(target=handle_client, args=(conn, addr)) + thread.start() \ No newline at end of file