Is there a standard way to make extra information available to handlers from the socketserver
module? The following is about the simplest solution I could think of, but it is unconvincing that this is the best way to make sure that handlers get access to some resources that they can all share.
#! /usr/bin/env python3
import socketserver
def main():
address = 'localhost', 10000
history = [b'Hello, world!']
server = CustomUDPServer(address, UDPHandler, history)
server.serve_forever()
class CustomUDPServer(socketserver.UDPServer):
def __init__(self, server_address, RequestHandlerClass, history):
super().__init__(server_address, RequestHandlerClass)
self.history = history
class UDPHandler(socketserver.DatagramRequestHandler):
def handle(self):
self.wfile.write(self.server.history[-1])
self.server.history.append(self.rfile.read())
if __name__ == '__main__':
main()