This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jimd
Recipients jimd
Date 2009-04-23.22:20:34
SpamBayes Score 2.942091e-15
Marked as misclassified No
Message-id <1240525238.53.0.858571250541.issue5824@psf.upfronthosting.co.za>
In-reply-to
Content
.../lib/python2.*/SocketServer.py in class DatagramRequestHandler
contains the following comment:
 
    # XXX Regrettably, I cannot get this working on Linux;
    # s.recvfrom() doesn't return a meaningful client address.

This is a poor way to document the limitation.  Someone writing code and
carefully adhering to the documentation will be presented with an
exception that stems from this and forced to read source code for the
standard libraries to understand why it's not behaving as documented.

In Issue1767511 it was recommended that the finish() method only call
sendto() if wfile.getvalue() returns a meaningful value.  (The bug here
is that this returns None under Linux and the default handler calls
sendto on it anyway).

Perhaps it would be better to raise an exception with a clear error
message ("Not supported on this platform") and to update the standard
documentation to clarify the need to over-ride the .finish() method if
one does NOT want a response automatically sent back.

(The fact that finish() does a sendto() seems like a poor default for a
UNIX domain socket --- for use with the UnixDatagramServer class --- in
any event.  That leads to a loop in .server_forever() where the instance
is reading its own response).

In fact the whole question of whether the DatagramRequestHandler should
be used with a UnixDatagramServer is reasonable.  If it's a nonsensical
combination then perhaps some sort of check could be implemented to
raise an exception ... or at least a sensible alternative should be
added to the standard documentation.

Example:

#!/usr/bin/env python
import SocketServer
usock = '/var/tmp/pong'

class PongHandler(SocketServer.DatagramRequestHandler):
    '''Play ping pong over datagram sockets
    '''
    def handle(self):
        '''Respond to any request with "Pong"
        '''
        pass
    def finish(self):
        '''Necessary under Linux to prevent:
           Exception:
           File "/usr/lib/python2.5/SocketServer.py", line 588, in finish
           self.socket.sendto(self.wfile.getvalue(), self.client_address)
           TypeError: argument must be string or read-only character
buffer, not
        '''
        if self.wfile.getvalue():
            SocketServer.DatagramRequestHandler.finish(self)

if __name__ == '__main__':
    SocketServer.UnixDatagramServer(usock, PongHandler).serve_forever()

... and testing this with:

#!/usr/bin/env python
import socket
USOCK = "/var/tmp/pong"
if __name__ == '__main__':
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    sock.sendto("Ping", USOCK)
History
Date User Action Args
2009-04-23 22:20:38jimdsetrecipients: + jimd
2009-04-23 22:20:38jimdsetmessageid: <1240525238.53.0.858571250541.issue5824@psf.upfronthosting.co.za>
2009-04-23 22:20:37jimdlinkissue5824 messages
2009-04-23 22:20:35jimdcreate