1

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.

  • It sounds like you are running `python listen.py` and `python server.py` in different processes. You need to have, probably, `server.py` start a thread for a function in `listen.py`. – quamrana Feb 03 '21 at 15:56
  • @quamrana thanks for your suggestion. I used `multiprocessing.pipe` to pass object between two processes and it's working fine! Also all code is contained in a single `server.py` file, so everything from `listen.py` is in `server.py` now. – bhushan pathak Feb 04 '21 at 08:00

1 Answers1

1

Here is how I did it using @quamrana's suggestion.

server.py

conn_list = []

def listen(child_pipe):
    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()
            child_pipe.send(vconn)
    except Exception as e:
        print(str(e))

if __name__ == '__main__':
    parent_pipe, child_pipe = multiprocessing.Pipe()
    p = multiprocessing.Process(target=listen, args=(child_pipe,))
    p.daemon = True
    p.start()
    while True:
        time.sleep(2)
        msg = parent_pipe.recv()
        if msg:
            conn_list.append(msg)
            print(conn_list)