0

I've got this virtually "Hello world" server:

import time
import zmq
import mutagen
import json

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:64108")

try:

    while True:
        #  Wait for next request from client
        message = socket.recv()
        jsn = message.decode('utf8')
        rq = json.loads(jsn)

        reply = 'Idle'

        if rq['request'] == 'settags':
            audio = mutagen.File(rq['file'], easy=True)
            if audio:
                reply = "track number set to [{}]".format(rq['tags']['tracknumber'])  # Debug info
                for tag, value in rq['tags'].items():
                    audio[tag] = value
                audio.save()
        #  Send reply back to client
        socket.send_string(reply)

except KeyboardInterrupt as e:
    sys.exit(e)

Any attempt but the very first ends up like this, quite naturally:

$ python /home/alexey/Dropbox/procrustes/procrserver.py 
Traceback (most recent call last):
  File "/home/alexey/Dropbox/procrustes/procrserver.py", line 20, in <module>
socket.bind("tcp://*:64108")
  File "zmq/backend/cython/socket.pyx", line 487, in zmq.backend.cython.socket.Socket.bind (zmq/backend/cython/socket.c:5156)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:7535)
zmq.error.ZMQError: Address already in use

All I want is to check the address, kill the abortive script, and get rid of the stack trace. Somehow I did not manage to find a usable answer. What am I supposed to do?

Alexey Orlov
  • 2,412
  • 3
  • 27
  • 46
  • So, you wish to spin up this process and have it run indefinitely, and then be able to attempt to spin it up again but fail gracefully if it's already running, is that correct? – Jason Jan 07 '16 at 18:30
  • [Check out this answer](http://stackoverflow.com/questions/7436801/identifying-listening-ports-using-python) to see if it gives you any usable ideas. ZMQ will not provide you a tool to check this, you'll either have to try/except to stop the exception, or you'll have to use some OS or general socket method to determine if a port is already being used. – Jason Jan 07 '16 at 18:39
  • Catching the exception sounds great, but what would be the right one? "...will not provide a tool" sounds a bit insane. If socket.bind may fail, there must be a way to handle the situation. – Alexey Orlov Jan 07 '16 at 18:59
  • ZMQ handles the situation by throwing an exception, same as any other socket library I've seen. ZMQ doesn't provide a tool because it might not be a ZMQ socket that has claimed the port, and there are plenty of existing methods within the language and the OS to check if a port is already in use (per the other answer I linked). As for what exception to handle, either handle all exceptions the same or use trial and error to discover the appropriate error code. – Jason Jan 07 '16 at 19:13
  • ```try: socket.bind() except: sys.exit()``` makes it silent, at least. A solution of sorts, yes. – Alexey Orlov Jan 07 '16 at 19:45

0 Answers0