Basic Python Question

Richard Spooner rspooner at
Thu Jul 22 11:41:28 CEST 2004


I'm very new to python and am trying to do the following.  I may get the
jargon wrong at times but hopefully you can see what I'm trying to do...

I have created a threaded class which sets up a socket and then binds to a
port.  When I make a new instance I send the port number and I would like
the __init__ routine to set up the socket and then attempt to bind it to a

If I already have an instance of this class running then obviously I'll
already have a port bound.  So if I try and create another instance with the
same port I'd like the program to flag this error and inform the user that
it is instead using the original instance..

So far I have this

import threading
import socket
import struct

class dataretriever(threading.Thread):
    def __init__(self, port):
        self.resultQueue = resultsQueue
        self.s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
            self.s.bind(( '', port))
            print "Error binding worker"

    def run(self):
        while 1:
            #playing with the data I recieve

x = dataretriever(9999)
y = dataretriever(9999)

When it tries to define y,  I'd like it to say "Port xxxx already in use by
x setting y = x"

Any ideas???

Thanks for your time, seems to be a good little language.


More information about the Python-list mailing list