I adapted the code below from the Python documentation here.
# filename: example.py
from datetime import datetime
import socket
import socketserver
import sys
import threading
import time
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
data = str(self.request.recv(1024), 'ascii')
cur_thread = threading.current_thread()
response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
self.request.sendall(response)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
allow_reuse_address = True
def client(ip, port, message):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((ip, port))
sock.sendall(bytes(message, 'ascii'))
response = str(sock.recv(1024), 'ascii')
print("Received: {}".format(response))
if __name__ == "__main__":
HOST, PORT = "127.0.0.1", 9999
if sys.argv[1] == "server":
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
with server:
ip, port = server.server_address
print(f'{ip} {port}')
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
# these work
client(ip, port, "Hello World 1 at " + str(datetime.now()))
time.sleep(1)
client(ip, port, "Hello World 2 at " + str(datetime.now()))
time.sleep(2)
client(ip, port, "Hello World 3 at " + str(datetime.now()))
if sys.argv[1] == "client":
# these do not work, why?
client(HOST, PORT, "Hello World 1 at " + str(datetime.now()))
time.sleep(1)
client(HOST, PORT, "Hello World 2 at " + str(datetime.now()))
time.sleep(2)
client(HOST, PORT, "Hello World 3 at " + str(datetime.now()))
Running this code as a server works as expected:
$ python3 example.py server
127.0.0.1 9999
Received: Thread-2: Hello World 1 at 2021-09-14 20:02:25.135218
Received: Thread-3: Hello World 2 at 2021-09-14 20:02:26.140889
Received: Thread-4: Hello World 3 at 2021-09-14 20:02:28.143664
I leave this terminal tab running (the program is waiting for additional clients to connect), open a new tab and run the following, but it produces an error:
$ python3 example.py client
Traceback (most recent call last):
File "/Users/ashroyer-admin/repo/phd-courses/2021-F-cloudcomp/thred.py", line 45, in <module>
client(HOST, PORT, "Hello World 1 at " + str(datetime.now()))
File "/Users/ashroyer-admin/repo/phd-courses/2021-F-cloudcomp/thred.py", line 20, in client
sock.connect((ip, port))
ConnectionRefusedError: [Errno 61] Connection refused