473,378 Members | 1,454 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,378 software developers and data experts.

Basic Python Question

Hey,

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
port.

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):
threading.Thread.__init__(self)
self.setDaemon(1)
self.resultQueue = resultsQueue
self.s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
try:
self.s.bind(( '', port))
except:
print "Error binding worker"
self.start()
def run(self):
while 1:
pass
#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.

Dave



Jul 18 '05 #1
4 1528
except:
print "Error binding worker"

I think this should be:
except:
print "Error binding worker"
raise

But maybe I did not understand your problem.

Jul 18 '05 #2
At the moment it detects the error and prints out "Error binding Worker"
just because it was something I could easily check was working.

I would like it to use the except to

check that it's a bind error,
find the existing instance that is bound to that port
make the new instance that it is attempting to create reference this
existing instance. Thus having the new instance effectively bound to the
port but in fact really linked to the existing instance which is in turn
bound to the port.

Cheers

Dave

"Gandalf" <ga*****@geochemsource.com> wrote in message
news:ma*************************************@pytho n.org...
except:
print "Error binding worker"

I think this should be:
except:
print "Error binding worker"
raise

But maybe I did not understand your problem.


Jul 18 '05 #3
On Thu, 22 Jul 2004, Richard Spooner wrote:
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
port.

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..


To get the effect you want (return the previous instance), you'll need to
do some trickery with __new__, the function which is responsible for
creating a class (as opposed to initialization, which is handled by
__init__):

import threading
import socket
import struct

class dataretriever(threading.Thread):
__bound = {} # this holds data of the form port:class_instance
__opened = False # used to stop multiple __init__s

def __new__(cls,port):

# try to get an existing class instance bound to port
try:
c = cls.__bound[port]

# if that fails, make a new instance
except KeyError:
c = threading.Thread.__new__(cls, port)
# and store it in our class variable
cls.__bound[port] = c

# this is executed if the try: succeeded
else:
print 'Port %d already bound, returning previous instance!' % port

# __new__ must return an instance of cls
return c

def __init__(self, port):

# only initialize ourselves once
if self.__opened: return True
self.__opened=True

threading.Thread.__init__(self)
self.setDaemon(1)
self.resultQueue = resultsQueue
self.s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
try:
self.s.bind(( '', port))
except:
print "Error binding worker"
self.start()

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

x = dataretriever(9999)
z = dataretriever(10000)
z is x False y = dataretriever(9999) Port 9999 lready bound, returning previous instance! y is x

True

Hope this all makes some sense :P

Jul 18 '05 #4
"Richard Spooner" <rs******@frisurf.no> wrote in message news:<6Z******************@news4.e.nsc.no>...
Hey,

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
port.

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):
threading.Thread.__init__(self)
self.setDaemon(1)
self.resultQueue = resultsQueue
self.s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
try:
self.s.bind(( '', port))
except:
print "Error binding worker"
self.start()
def run(self):
while 1:
pass
#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.

Dave


Maybe not a big hit but could be an approach:
class dataretriever(threading.Thread): .... __port = None
.... __instance = None
.... def __init__(self,port):
.... if port == dataretriever.__port:
.... print 'Error binding worker'
.... self.__dict__ = dataretriever.__instance.__dict__
.... return
.... else:
.... dataretriever.__port = port
.... dataretriever.__instance = self
.... self.do_what_ever_you_want_to_do()
.... def do_what_ever_you_want_to_do(self):
.... print 'I do it'
.... a=dataretriever(2) I do it b=dataretriever(2) Error binding worker id(a) 12281616 id(b) 14817928 a.foo='bar'
b.foo 'bar'


Regards
Peter
Jul 18 '05 #5

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

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
3
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello I am teaching myself python, and I have gotten a long way, it's quite a decent language and the syntax is great :) However I am having a...
5
by: Aaron Ginn | last post by:
I'm investigating the feasibility of using Python instead of Visual Basic for a commercial software package that I'm planning on developing. Now I'm absolutely a Python zealot. I use it for most...
9
by: abisofile | last post by:
hi I'm new to programming.I've try a little BASIC so I want ask since Python is also interpreted lang if it's similar to BASIC.
6
by: aghazalp | last post by:
hi guys, this would be the most basic question ever...I am not a programmer but I am trying to learn programming in python...I was reading John Zelle's text book and instructed me to make .py file...
1
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given...
4
by: Hoop | last post by:
Hi, I have been working in getting Boost.Python running on my PC, seems to work now. I have what I believe is somewhat of basic question here. I am starting on an application that will...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
2
by: Dave Dean | last post by:
Hi all, I'm just starting out in sockets/network programming, and I have a very basic question...what are the 'security' implications of opening up a socket? For example, suppose I've written a...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.