Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unbinding sockets in threads..

Reply
Thread Tools

Unbinding sockets in threads..

 
 
Richard Spooner
Guest
Posts: n/a
 
      08-03-2004
Chaps,

I've written a piece of python code below that when called with a line such
as x = getdata(9999), listens on that port for data I'm sending it and puts
the data in a list. If I delete x using del x and then try and assign a new
value to port 9999 (e.g z=getdata(9999) ) then it reports that the socket it
already bound, and I can't find any way of unbinding the port.

If I try it not in a thread the del function works fine (i.e. creating and
binding a socket s and then deleting it) Can anyone suggest some
modifications I can do to make this work??

Thanks in advance

Dave

import threading
import socket
import struct
import sys
import time





class getdata(threading.Thread):
def __init__(self, port):
threading.Thread.__init__(self)
self.x = ['ZERO SPACE']
self.s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
try:
self.s.bind(( '', port))

except socket.error, msg:
print "Could not bind to socket"
time.sleep(3)
self.s.close()
sys.exit(1)
self.carryon = 'true'
self.start()






def run(self):
while self.carryon == 'true':
self.data, self.addr = self.s.recvfrom( 1024 )
self.channel, self.value = struct.unpack( 'IBxxx' , self.data )
try:
self.x[int(self.channel)] = self.value
except IndexError:
while len(self.x)-1 < int(self.channel):
if len(self.x) == int(self.channel):
self.x.append(self.value)
else:
self.x.append('NA')
except:
print "Dictionary creation error"
time.sleep(3)
self.s.close()
sys.exit

print self.x
return


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tkinter unbinding Roger Python 17 12-04-2011 11:13 PM
Unbinding Tkinter default bindings for Listbox Roger Python 7 01-13-2009 01:13 AM
Unbinding multiple variables Johnny Lin Python 11 01-24-2005 08:27 PM
Re: UNbinding a socket Sin C++ 2 06-25-2003 06:27 PM
Re: (OT) UNbinding a socket Michael Furman C++ 0 06-23-2003 07:41 PM



Advertisments