def main(): # The 'foo' site is protected by a password foo_root = resource.Resource() foo_root.putChild("foo", FooResource()) realm = HttpPasswordRealm(foo_root) checker = checkers.InMemoryUsernamePasswordDatabaseDontUse() checker.addUser("tom", "tom") my_portal = portal.Portal(realm, [checker]) credentialFactory = BasicCredentialFactory("StreamProx") protected_foo_root = HTTPAuthSessionWrapper(my_portal, [credentialFactory]) # The 'bar' site is unprotected bar_root = resource.Resource() bar_root.putChild("bar", BarResource()) # Create two web sites site1 = server.Site(protected_foo_root) site2 = server.Site(bar_root) # Create a StreamProx server. Customize its packet buffer and dispatcher. factory = BufferingProxyFactory() factory.buffer_factory = PacketBuffer ExampleDispatcher.site1 = site1 ExampleDispatcher.site2 = site2 factory.dispatcher_factory = ExampleDispatcher reactor.listenTCP(8080, factory) log.startLogging(sys.stdout) reactor.run()
def run(self, options): """Run the app. Options as parsed by optparse.""" log.startLogging(sys.stdout) cfg = config.Config(options) instances = InstanceCollection(cfg) # website log.msg('Build as site object:') dispatcher = get_dispatcher(cfg, instances) website = Site( dispatcher ) log.msg('\t\t\t\t\t=> OK') # websockets # Somehow the websocket server needs to be commanded to send updated # state data. # Sending a keepalive might be good too log.msg('Setup TCP port:') factory = BufferingProxyFactory() factory.buffer_factory = PacketBuffer websocketfactory = WebSocketFactory(StreamFactory(dispatcher, instances)) # route /ws to websockets, everything else including / to http ExampleDispatcher.prefix1 = "/ws" ExampleDispatcher.site1 = websocketfactory ExampleDispatcher.prefix2 = "/" ExampleDispatcher.site2 = website factory.dispatcher_factory = ExampleDispatcher reactor.listenTCP(cfg.port, factory, interface=cfg.bind_address) log.msg('\t\t\t\t\t=> OK') log.msg('Run reactor:') reactor.run()
# - open the Javascript console to see the echo text # - click the [Send] button # if __name__ == '__main__': log.startLogging(sys.stdout) # Set up an Autobahn echo server ws = WebSocketServerFactory("ws://localhost:8080", debug = True) ws.protocol = EchoServerProtocol root = resource.Resource() root.putChild("demo", DemoResource()) site = server.Site(root) factory = BufferingProxyFactory() factory.buffer_Factory = PacketBuffer # Route /demo urls to our website ExampleDispatcher.prefix1 = "/demo" ExampleDispatcher.site1 = site # Route "/" to the Autobahn websocket server ExampleDispatcher.prefix2 = "/" ExampleDispatcher.site2 = ws factory.dispatcher_factory = ExampleDispatcher reactor.listenTCP(8080, factory) reactor.run()
# Open a browser to http://localhost:8080/demo # It will render the local demo page and proxy a websocket to a remote host: echo.websocket.org if __name__ == '__main__': log.startLogging(sys.stdout) # The local website serves the webpage root = resource.Resource() root.putChild("demo", DemoResource()) site = server.Site(root) # Customize the default packet buffer class to look for the entire header chunk PacketBuffer.delimiter = "\r\n\r\n" # Construct our proxy and configure it factory = BufferingProxyFactory() factory.buffer_factory = PacketBuffer # Route /demo urls to our local website ExampleDispatcher.prefix1 = "/demo" ExampleDispatcher.site1 = site # Install our custom dispatcher to connect to the remote WebSocket server factory.dispatcher_factory = RewritingExampleDispatcher reactor.listenTCP(8080, factory) reactor.run()