From 2818b67cd5bbdc0d27f20664b4a1414e016e49dd Mon Sep 17 00:00:00 2001 From: marc Date: Fri, 8 Jan 2021 02:27:33 +0100 Subject: [PATCH] socket server --- ]kj | 47 +++++++++++++++++++++++++++++++++++++ client.py | 1 + collatz.py | 3 +++ commands.py | 26 +++++++++++++++++++++ hi.py | 0 loop.py | 7 ++++++ parrallel-processing.py | 4 ++++ socket_client.py | 25 ++++++++++++++++++++ socket_server.py | 51 +++++++++++++++++++++++++++++++++++++++++ tasks.py | 13 +++++++++++ 10 files changed, 177 insertions(+) create mode 100644 ]kj create mode 100644 client.py create mode 100755 collatz.py create mode 100755 commands.py mode change 100644 => 100755 hi.py create mode 100755 loop.py create mode 100755 parrallel-processing.py create mode 100755 socket_client.py create mode 100755 socket_server.py create mode 100644 tasks.py diff --git a/]kj b/]kj new file mode 100644 index 0000000..22d8d1b --- /dev/null +++ b/]kj @@ -0,0 +1,47 @@ +#!/usr/bin/env python +""" +Socket server. +""" + +import socket +import threading + +HEADER = 64 +PORT = 5050 +SERVER = socket.gethostbyname(socket.gethostname()) +ADDR = (SERVER, PORT) +FORMAT = 'utf-8' +DISCONNECT_MESSAGE = '!DISCONNECT' + +server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +server.bind(ADDR) + +def handle_client(conn, addr): + """Deal with one client.""" + print(f"[NEW CONNECTION] {addr} connected.") + + while True: + msg_length = conn.recv(HEADER).decode(FORMAT) + msg_length = int(msg_length) + msg = conn.recv(msg_length).decode(FORMAT) + if msg == DISCONNECT_MESSAGE: + break + + print(f"[{addr}] {msg}") + conn.close() + + + + +def start(): + """Start the server.""" + server.listen() + while True: + conn, addr = server.accept() + thread = threading.Thread(target=handle_client, args=(conn, addr)) + thread.start() + print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}") + +if __name__ == "__main__": + print("[STARTING] server is starting ...") + start() diff --git a/client.py b/client.py new file mode 100644 index 0000000..d325550 --- /dev/null +++ b/client.py @@ -0,0 +1 @@ +:q diff --git a/collatz.py b/collatz.py new file mode 100755 index 0000000..a14108c --- /dev/null +++ b/collatz.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python +import itertools as it; collatz = lambda n:len(list(it.takewhile(lambda n:n>1, (n:=(n//2 if n%2==0 else 3*n+1) for a in it.repeat(1)))))+1 +print(collatz(10)) diff --git a/commands.py b/commands.py new file mode 100755 index 0000000..d4c4f97 --- /dev/null +++ b/commands.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +import sys +import os +import yaml + +from rich import print + +if not 'commands.yml' in os.listdir(): + print(f"File [bold]commands.yml[/bold] not found in \ + [bold]{os.getcwd()}[/bold]!") + quit() + +if len(sys.argv)==1: + print("[bold]No command[\bold] given as input.") + quit() + +with open('commands.yml') as f: + commands = yaml.load(f, Loader=yaml.FullLoader) + +command = sys.argv[1] +if not command in commands: + print(f"Command [bold]{command}[\bold] does not exist.") + quit() + +code = commands[command] +os.system(code) diff --git a/hi.py b/hi.py old mode 100644 new mode 100755 diff --git a/loop.py b/loop.py new file mode 100755 index 0000000..8d3ff6e --- /dev/null +++ b/loop.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python +import sys +import time + +while True: + print(sys.argv[1]) + time.sleep(1) diff --git a/parrallel-processing.py b/parrallel-processing.py new file mode 100755 index 0000000..442113d --- /dev/null +++ b/parrallel-processing.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python + +import multiprocessing as mp +print("Number of processors: ", mp.cpu_count()) diff --git a/socket_client.py b/socket_client.py new file mode 100755 index 0000000..3fae22e --- /dev/null +++ b/socket_client.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +import socket +import threading + +HEADER = 64 +PORT = 5050 +FORMAT = 'utf-8' +HOST = socket.gethostbyname("localhost") +DISCONNECT_MESSAGE = "!DISCONNECT" + +ADDR = (HOST, PORT) +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect(ADDR) + +def send(msg): + message = msg.encode(FORMAT) + msg_length = len(message) + send_length = str(msg_length).encode(FORMAT) + send_length += b' ' * (HEADER - len(send_length)) + client.send(send_length) + client.send(message) + +send('test') +send(DISCONNECT_MESSAGE) diff --git a/socket_server.py b/socket_server.py new file mode 100755 index 0000000..d97ee50 --- /dev/null +++ b/socket_server.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +""" +Socket HOST. +""" + +import socket +import threading + +HEADER = 64 +PORT = 5050 +# HOST = socket.gethostbyname(socket.gethostname()) +print("before get host by name") +HOST = socket.gethostbyname('localhost') +print(HOST) +ADDR = (HOST, PORT) +FORMAT = 'utf-8' +DISCONNECT_MESSAGE = '!DISCONNECT' + +HOST = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +HOST.bind(ADDR) + +def handle_client(conn, addr): + """Deal with one client.""" + print(f"[NEW CONNECTION] {addr} connected.") + while True: + msg_length = conn.recv(HEADER).decode(FORMAT) + if not msg_length: + continue + msg_length = int(msg_length) + msg = conn.recv(msg_length).decode(FORMAT) + print(f"[{addr}] {msg}") + if msg == DISCONNECT_MESSAGE: + break + conn.send("Msg received".encode(FORMAT)) + + conn.close() + print(f"[NEW CONNECTION] {addr} disconnected.") + +def start(): + """Start the HOST.""" + HOST.listen() + print("[LISTENING] HOST listening on {HOST}") + while True: + conn, addr = HOST.accept() + thread = threading.Thread(target=handle_client, args=(conn, addr)) + thread.start() + print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}") + +if __name__ == "__main__": + print("[STARTING] HOST is starting ...") + start() diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..8f598c9 --- /dev/null +++ b/tasks.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +from time import sleep +from rich.console import Console + +console = Console() +tasks = [f"task {n}" for n in range(1, 11)] + +with console.status("[bold green]Working on tasks...") as status: + while tasks: + task = tasks.pop(0) + sleep(1) + console.log(f"{task} complete")