I have been working with python server and client communication. In order to establish a connection, usually, the server needs to run 1st and then client from separate python scripts.
What I want now is to make it automated. I want to run both server and client from a single python script or a GUI button. I have been trying different ways like multiprocessing and multithreading but it is not working.
Please advice.
Sample server and client codes below:
Server.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 5005))
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
print("received data",list(data))
if not data:
break
conn.sendall(data)
conn.close()
Client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('0.0.0.0', 5005))
message = bytearray([1])
s.sendall(message)
data = s.recv(1024)
s.close()
print ('Received', repr(data))