UDP Client/Server

Guilherme Polo ggpolo at gmail.com
Tue Jan 22 18:55:32 EST 2008


2008/1/22, Martin Marcher <martin at marcher.name>:
> Hello,
>
> I created a really simple udp server and protocol but I only get every 2nd
> request (and thus answer just every second request).
>
> Maybe someone could shed some light, I'm lost in the dark(tm), sorry if this
> is a bit oververbose but to me everything that happens here is black magic,
> and I have no clue where the packages go. I can't think of a simpler
> protocol than to just receive a fixed max UDP packet size and answer
> immediately (read an "echo" server).
>
> thanks
> martin
>
>
> ### server
> >>> from socket import *
> >>> import SocketServer
> >>> from SocketServer import BaseRequestHandler, UDPServer
> >>> class FooReceiveServer(SocketServer.UDPServer):
> ...     def __init__(self):
> ...             SocketServer.UDPServer.__init__(self, ("localhost", 4321),
> FooRequestHandler)
> ...
> >>> class FooRequestHandler(BaseRequestHandler):
> ...     def handle(self):
> ...             data, addr_info = self.request[1].recvfrom(65534)

Your FooReceiveServer subclasses UDPServer, it already handled the
recvfrom for you, so, this is wrong.

> ...             print data
> ...             print addr_info
> ...             self.request[1].sendto("response", addr_info)
> ...
> >>> f = FooReceiveServer()
> >>> f.serve_forever()
> request 0
> ('127.0.0.1', 32884)
> request 1
> ('127.0.0.1', 32884)
> request 2
> ('127.0.0.1', 32884)
> request 2
> ('127.0.0.1', 32884)
> request 2
> ('127.0.0.1', 32884)
>
>
>
> ### client
> >>> target = ('127.0.0.1', 4321)
> >>> from socket import *
> >>> s = socket(AF_INET, SOCK_DGRAM)
> >>> for i in range(10):
> ...     s.sendto("request " + str(i), target)
> ...     s.recv(65534)
> ...
> 9
> Traceback (most recent call last):
>   File "<stdin>", line 3, in <module>
> KeyboardInterrupt
> >>> s.sendto("request " + str(i), target)
> 9
> >>> str(i)
> '0'
> >>> for i in range(10):
> ...     s.sendto("request " + str(i), target)
> ...     s.recv(65534)
> ...
> 9
> 'response'
> 9
> 'response'
> 9
> Traceback (most recent call last):
>   File "<stdin>", line 3, in <module>
> KeyboardInterrupt
> >>> #this was hanging, why?
> ...
> >>> s.sendto("request " + str(i), target)
> 9
> >>> s.recv(65534)
> 'response'
> >>> s.sendto("request " + str(i), target)
> 9
> >>> s.recv(65534)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyboardInterrupt
> >>> s.sendto("request " + str(i), target)
> 9
> >>> s.sendto("request " + str(i), target)
> 9
> >>> s.recv(65534)
> 'response'
> >>> s.recv(65534)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> KeyboardInterrupt
> >>> s.sendto("request " + str(i), target)
> 9
> >>>
>
> --
> http://noneisyours.marcher.name
> http://feeds.feedburner.com/NoneIsYours
>
> You are not free to read this message,
> by doing so, you have violated my licence
> and are required to urinate publicly. Thank you.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list