Strange UDPServer behaviour
Peter Hansen
peter at engcorp.com
Mon Mar 31 09:01:14 EST 2003
Ulf von Ceumern wrote:
>
> However, whenever i send a udp datagram to the server and print the
> request, i get 5 times the same message.
>
> -----------------
> the test-client:
> -----------------
>
> import socket
>
> spamHosts=("127.0.0.1")
Did you intend to make this a tuple? It's not a tuple right now...
To make a one-element tuple, you must add a trailing comma, to
let the compiler know you didn't mean for this just to be an
expression in parentheses:
spamHosts = ("127.0.0.1", ) # trailing comma required here
A "better" solution is to always use lists when you are expecting
to have a list of things, even if you don't plan to change the
contents of the list. Use tuples to represent "static collections
of different data types", as with C structs. (In spite of Guido's
reported recent suggestions to the contrary, where he apparently
didn't encourage this view of lists and tuples.)
spamHosts = ["127.0.0.1"] # trailing comma is optional in lists
> spamPort=5456
>
> class spamlog:
> def __init__(self):
> self.spamSocket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>
> def send(self,f,ip):
> for ipdest in spamHosts:
> self.spamSocket.sendto("%s|%s"%(f,ip),(ipdest,spamPort))
Above, you are probably iterating one character at a time through your
string, rather than once through a tuple.
> ----------------
> the output when i call udpsend.py once:
> ----------------
> bash-2.03$ ./testd.py
> heinbloed at kutter.de|192.192.192.192
> heinbloed at kutter.de|192.192.192.192
> heinbloed at kutter.de|192.192.192.192
> heinbloed at kutter.de|192.192.192.192
> heinbloed at kutter.de|192.192.192.192
> -----------------
> why 5 times??? this is driving me crazy.
Probably it would have gone for len("127.0.0.1") times, except for
something like the backlog in TCP (is there anything similar in UDP?)
but I doubt it's directly relevant to the problem.
I *suspect* this is what went wrong, but I haven't tried your
code to test the theory nor, I'm afraid, really analyzed the
rest of your code to see if this is likely.
-Peter
More information about the Python-list
mailing list