I was trying to develop a client server module where I have 2 files listen.py and server.py for server side and client.py for client side.
Now, I am trying to implement multi client server and thus I have 2 separate files where listen.py creates a socket object and listens for connections, and once client is connected its socket object will be appended to a list and that updated list should be accessible in server.py
code & output for both files:
listen.py
import socket
conn_list = []
if __name__ == '__main__':
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(1)
sock.bind(('0.0.0.0',80))
print('started listening on port 80')
sock.listen(10)
while True:
vconn,addr = sock.accept()
print('Got connection from : ',addr)
conn_list.append(vconn)
output of listen.py (hiding ip):
started listening on port 80
Got connection from : ('x.x.x.x', 64486)
[<socket.socket fd=916, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('y.y.y.y', 80), raddr=('x.x.x.x', 64486)>]
and in server.py I am just trying to print updated conn_list:
import time
import listen
if __name__ == '__main__':
while True:
time.sleep(5)
print(listen.conn_list)
output of server.py:
[]
[]
[]
[]
[]
[]
[]
[]
As we can see, list is not getting updated as I get an empty list. I have even tried to reload module "listen" in server.py using reload library but with no luck as reload actually reloads static elements!
All I want to do is have that socket object in server.py for further operations.
Platform: Windows 10
Python: 3.8.5
Any help is appreciated.