473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Unbinding sockets in threads..

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
Jul 18 '05 #1
0 1118

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
4
by: Julia Goolia | last post by:
hello, i read that it is bad to use threads with tkinter. so my question is how does one create a gui program with sockets? at one point you have to call mainloop() which does not return. ...
0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
11
by: Steven | last post by:
Hi, I need to write an application using sockets. I have a server and about 10 clients "speaking" at the same time with the server, so i guess i need to use asynchronous sockets. But the server...
4
by: nyhetsgrupper | last post by:
Hi, I've written a async server app. This app start by connecting to a client and then send some data (BeginSend). When the data is sent, the server is starting to listen for incomming data....
6
by: Richard | last post by:
Hi All, I don't know in which group my question needs to be posted so here i go: I know that the socket 'begin...' methods uses the threadpool to call the callback function but does that mean...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.