1

I have python project. The project is listening a port. Here is my code:

import socket

conn = None     # socket connection

###
#   port listener
#   @return nothing
###
def listenPort():
     global conn
     conn = socket.socket()
     conn.bind(("", 5555))
     conn.listen(5)

After run my app I checked with hercules for port connection. It works but I disconnected and again connected. after doing it 5 time connection return error. I want to the listener must works always. How can I got app like I want ? Thanks in advance!

EDIT

I will run my app only on server and I will check by Uptime root listener is working.

Ulug'bek
  • 2,762
  • 6
  • 31
  • 59
  • I saw [that question](http://stackoverflow.com/questions/25609467/python-one-liner-silent-socket-listener), but I didn't understand anything. Maybe It is not What I search – Ulug'bek Feb 04 '15 at 10:27

1 Answers1

1

The error is normal :

  • you listen to a socket with a queue of size 5
  • you never accept any connection

=> you enqueue 5 connection requests and the 6th causes an error.

You must accept a request to remove it form the listen queue and work with it (the command accepter = conn.accept() derived from related post)

Edit

Here is a fully functionnal example :

def listenPort():
    global conn
    conn = socket.socket()
    conn.bind(("", 5555))
    conn.listen(5)
    while True:
        s, addr = conn.accept() # you must accept the connection request
        print("Connection from ", addr)
        while True:  # loop until othe side shuts down or close the connection
            data = s.recv(17)   # do whatever you want with the data
            # print(data)
            if not data:  # stop if connection is shut down or closed
                break
            # for example stop listening where reading keyword "QUIT" at begin of a packet
            # elif data.decode().upper().startswith("QUIT"):
            #     s.close()
            #     conn.close()
            #     return
        s.close() # close server side
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Now I got error `accepter = conn.accept() File "C:\Python32\lib\socket.py", line 132, in accept fd, addr = self._accept() socket.error: [Errno 10022] Received an invalid argument` – Ulug'bek Feb 04 '15 at 10:56
  • @UlugbekKomilovich This is a weird error : I've just looked at the manual and the syntax is indeed `fd, addr = conn.accept()` where `conn` is a bound and listening socket. I've currently no access to my computer, but I'll try that in a while. – Serge Ballesta Feb 04 '15 at 13:13
  • @UlugbekKomilovich : I could test and it works. Server side : `conn = socket.socket() - conn.bind(("", 5555")) - conn.listen(5) - conn.accept()` (` - ` standing for new line) - client side : `s = socket.socket() - s.connect(("localhost, 5555))` . Server side `accept` waits until client side `connect`. – Serge Ballesta Feb 04 '15 at 17:49
  • I just want to listener in server side and I don't care about client side. I use [uptime root](https://uptimerobot.com/) to check my listener is working or not – Ulug'bek Feb 05 '15 at 05:45
  • @UlugbekKomilovich : I've added an example tested under python 3.4 – Serge Ballesta Feb 05 '15 at 12:52
  • @UlugbekKomilovich : the error you showed in first comment was probably caused by calling `accept` before `listen` – Serge Ballesta Feb 05 '15 at 16:00