From 27acbb2be397bc7ba070d2442c144e1647b046e3 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Wed, 14 Oct 2020 22:07:45 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 +++ README.md | 8 ++++++++ echo_client.py | 27 +++++++++++++++++++++++++++ echo_server.py | 35 +++++++++++++++++++++++++++++++++++ socket_server_demo.py | 21 +++++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 echo_client.py create mode 100644 echo_server.py create mode 100644 socket_server_demo.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffe7c5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +__pycache__/ +*.pyc diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa7b868 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# sockets-example + +This project is just me trying to remediate myself on more advanced python topics. There are a few variant iterations of this example floating around the internet. But my example is unique for three reasons: + +1. I bundle the server and client into proper classes. Most people just give you the "more pythonic" flat method technique. I think this is questionable. +2. I provide a runner that handles everything. Starts the server, starts the client, passes messages between them, and logs it all to the console. +3. I use multiprocessing to be able to start the server and run the client in the same demo script. + diff --git a/echo_client.py b/echo_client.py new file mode 100644 index 0000000..8a2dab3 --- /dev/null +++ b/echo_client.py @@ -0,0 +1,27 @@ +import socket +from time import sleep + + +class EchoClient: + HOST = '127.0.0.1' # The server's hostname or IP address + PORT = 65432 # The port used by the server + + def __init__(self, host=HOST, port=PORT): + self._s = socket.socket() + self._s.connect((host, port)) + print("Client initialized.") + + def echo(self, message="Hello World!"): + print("Client will send data: '{}'".format(message)) + self._s.sendall(message.encode()) + data = self._s.recv(4096) + response = data.decode() + print("Client received data from server: {}".format(response)) + if message == "!quit": + print("Client terminating.") + self.close() + quit(0) + return repr(response) + + def close(self): + self._s.close() diff --git a/echo_server.py b/echo_server.py new file mode 100644 index 0000000..3d4818f --- /dev/null +++ b/echo_server.py @@ -0,0 +1,35 @@ +import socket +from multiprocessing import Process +from random import randint +from os import getpid +from time import sleep + + +class EchoServer: + HOST = '127.0.0.1' # Standard loopback interface address (localhost) + PORT = 65432 # Port to listen on (non-privileged ports are > 1023) + + def __init__(self, host=HOST, port=PORT): + self._host = host + self._port = port + print("Server Initializing on Host({0}) and Port({1})".format(self._host, self._port)) + sock = socket.socket() + sock.bind((self._host, self._port)) + sock.listen(1) + print("Server Listener initialized") + conn, addr = sock.accept() + print('Server Connection From Client:', addr) + while True: + data = conn.recv(4096).decode() + print("Server received data from client {0}: {1}".format(str(addr), str(data))) + if data == "!quit": + print("Server Terminating") + conn.sendall(data.encode()) + break + response = "Received: " + data + conn.sendall(response.encode()) + conn.close() + + +if __name__ == '__main__': + EchoServer() diff --git a/socket_server_demo.py b/socket_server_demo.py new file mode 100644 index 0000000..52eeece --- /dev/null +++ b/socket_server_demo.py @@ -0,0 +1,21 @@ +from multiprocessing.context import Process +from multiprocessing.spawn import freeze_support +from time import sleep + +from echo_client import EchoClient +from echo_server import EchoServer + +if __name__ == '__main__': + # freeze_support() + s = Process(target=EchoServer) + s.start() + sleep(1) # need to figure out a way to make this intelligent. + c = EchoClient() + c.echo("Now is the time for all good men.") + c.echo("You load 14 tons, and what do you get?") + c.echo("!quit") + + + + +