ThreadingUDPSocketServer drop/reject?

James R. Saker Jr. jsaker at americanrelay.com
Fri Sep 24 16:18:56 CEST 2004


Didn't see a reply to my request but I figured it out using
DatagramRequestHandler under SocketServer:


"USAGE: %s <port>"
from SocketServer import DatagramRequestHandler, UDPServer
from sys import argv

class EchoHandler(DatagramRequestHandler):

   def handle(self):

      ## Assume following three addresses are legit; var placed
      ## here for simplicity in test; would be referred from elsewhere
      ## in real instance instead of local 'addrList'

      addrList = ['192.168.1.1', '192.168.1.2', '192.168.1.3']

      if self.client_address[0] in addrList:
        print "Client connected:", self.client_address
        message = self.rfile.read()
        print "Message was: %s" % message
        self.wfile.write(message)
      else:
         print "Rejected %s" % self.client_address[0]

if len(argv) != 2:
   print __doc__ % argv[0]
else:
   UDPServer(('',int(argv[1])), EchoHandler).serve_forever()





Result: Hosts in addrList can access echo server; hosts not in addrList
are rejected (not sure if this a UDP drop or reject; need to look into
further I'd expect on detailed UDP drop/reject behavior.

Jamie





More information about the Python-list mailing list