Unbinding sockets in threads..

Discussion in 'Python' started by Richard Spooner, Aug 3, 2004.

  1. 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
     
    Richard Spooner, Aug 3, 2004
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sin

    Re: UNbinding a socket

    Sin, Jun 23, 2003, in forum: C++
    Replies:
    2
    Views:
    14,533
  2. Johnny Lin

    Unbinding multiple variables

    Johnny Lin, Jan 21, 2005, in forum: Python
    Replies:
    11
    Views:
    846
    Johnny Lin
    Jan 24, 2005
  3. Roger

    Tkinter unbinding

    Roger, Dec 18, 2008, in forum: Python
    Replies:
    17
    Views:
    3,339
    fabien
    Dec 4, 2011
  4. Roger
    Replies:
    7
    Views:
    927
    Roger
    Jan 13, 2009
  5. Mark Tarver

    unbinding a global variable in Python

    Mark Tarver, Apr 30, 2009, in forum: Python
    Replies:
    4
    Views:
    1,375
    Peter Otten
    Apr 30, 2009
  6. Grigory Javadyan
    Replies:
    1
    Views:
    311
    Peter Otten
    Feb 20, 2011
  7. Mark

    Unbinding Dropdown after Bind?

    Mark, Jan 6, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    278
  8. Eric Mahurin

    unbinding a Proc?

    Eric Mahurin, Jun 5, 2005, in forum: Ruby
    Replies:
    5
    Views:
    312
    Logan Capaldo
    Jun 5, 2005
Loading...