commit
27acbb2be3
5 changed files with 94 additions and 0 deletions
@ -0,0 +1,3 @@ |
||||
.idea/ |
||||
__pycache__/ |
||||
*.pyc |
@ -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. |
||||
|
@ -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() |
@ -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() |
@ -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") |
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue