Errno 111 Connection denied Python socket programming

I have two python scripts, one server.py and one worker.py This should work like this:

- server.py listens on 5005
- workers are started and bind to random ports
- they send a message to the server with their port nr
- server adds the port to its list of known workers
- it does this for each worker

      

The problem is that after the first worker is added and I run another one, I get this error on the server side:

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "server.py", line 42, in listener
    if handle_join(data.split(',')[1:]) == 1:
  File "server.py", line 122, in handle_join
    s.connect(("",int(worker_ip_port[1])))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused

      

The app has grown quite a bit, so I will only include the following server.py sections:

def handle_join(worker_ip_port):
# e.g. worker_ip_port = 127.0.0.1,55256
# 
#
    worker_ip_port = tuple(worker_ip_port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        print "Trying to connect to {}".format(worker_ip_port)
        s.connect(("",int(worker_ip_port[1])))
    if worker_ip_port not in WORKERS:
        WORKERS.append(worker_ip_port)
        print '\t\tNew worker added on {}'.format(worker_ip_port)
        print '\t\tWORKERS: {}'.format(WORKERS)
                s.send('0'+SIGEND)
                s.close()
                return 0
    print '\t\tThat port is already in the worker list'
    s.send('1'+SIGEND)
    s.close()
    return 1

      

worker.py

def read_socket():
# binds socket, keeps listening
# once connection is accepted, loops over the socket buffer until the
# signal for transmission end
# then returns the data
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   s.bind(("",WORKER_PORT))
   print "listening on {}".format(WORKER_PORT)
   while True:
           buffer = ''
           data = True
           s.listen(0)
           conn, addr = s.accept()
           print "accepted connection"
           while data:
                    data = conn.recv(BUFFER_SIZE)
                    # if the SIGNAL for end of packet is found in current packet
                    # add only up to that part
                    # close socket
                    # return data
                    if data.find(SIGEND) != -1:
                            buffer += data[:data.rfind(SIGEND)]
                            conn.close()
                            s.close()
                            return buffer
                    else:
                            buffer += data

      

NOTES: I've tried both Linux and Windows. I read all the other suggestions on the site and what I could find on google. None of them worked.

Tried telnet to the connection established by the second worker:

Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

      

From what I could tell the worker SECOND is not getting any connection to the server.

The output from netstat is also interesting:

tcp        0      0 0.0.0.0:55271           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:5005          127.0.0.1:55271         TIME_WAIT

      

55271 is the port to which SECOND is bound; below is the connection it uses to send the "I AM HERE" message to the server;

tcp        0      0 0.0.0.0:55269           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:59240         127.0.0.1:55269         TIME_WAIT
tcp        0      0 127.0.0.1:5005          127.0.0.1:55269         TIME_WAIT

      

In the case of the FIRST worker (here on site 55269) I see there are two connections. As it should be. The first one is from the worker to the server, with "I AM HERE". And the second, from the server to the worker, by sending a confirmation that everything is fine and that it is now on the worklist.

I am using a multiprocessing framework. Will this be a problem?

+3


source to share


1 answer


solvable. Added try, except for server.py like:



def handle_join(worker_ip_port):
    worker_ip_port = tuple(worker_ip_port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        print "Trying to connect to {}".format(worker_ip_port)
        for i in range(3):
                try:
                        s.connect(("",int(worker_ip_port[1])))
                        if worker_ip_port not in WORKERS:
                                WORKERS.append(worker_ip_port)
                                print '\t\tNew worker added on {}'.format(worker_ip_port)
                                print '\t\tWORKERS: {}'.format(WORKERS)
                                s.send('0'+SIGEND)
                                s.close()
                                return 0
                except Exception as e:
                        pass
    print '\t\tThat port is already in the worker list'
        s.send('1'+SIGEND)
        s.close()
    return 1

      

0


source







All Articles