def test_leafResource(self): """ L{getChildForRequest} returns the first resource it encounters with a C{isLeaf} attribute set to C{True}. """ request = DummyRequest([b"foo", b"bar"]) resource = Resource() resource.isLeaf = True result = getChildForRequest(resource, request) self.assertIdentical(resource, result)
def wrapper(fn): """ :type path: str """ r = Resource() r.render_GET = fn r.isLeaf = True self.routes[path] = r return fn
def on_login(_): logger.info("Logged in as '%s'", spotify.session.user_name) root = Resource() root.isLeaf = False player = Player(spotify) root.putChild("player",player) root.putChild("static",File(cfg.get("SERVER", "static"))) site = Site(root) # setup and register service reactor.listenTCP(port, site) logger.info("Listening on %s:%d", host, port) zeroconf.register_service(service_info)
def test_postPathToPrePath(self): """ As path segments from the request are traversed, they are taken from C{postpath} and put into C{prepath}. """ request = DummyRequest([b"foo", b"bar"]) root = Resource() child = Resource() child.isLeaf = True root.putChild(b"foo", child) self.assertIdentical(child, getChildForRequest(root, request)) self.assertEqual(request.prepath, [b"foo"]) self.assertEqual(request.postpath, [b"bar"])
def make_real_webserver(self): """ Construct a real webserver to test actual connectivity. """ root = Resource() root.isLeaf = True root.render = lambda r: self._render_request(r) site_factory = Site(root) webserver = yield reactor.listenTCP( 0, site_factory, interface='127.0.0.1') self.add_cleanup(webserver.loseConnection) addr = webserver.getHost() url = "http://%s:%s/" % (addr.host, addr.port) returnValue(url)
def render_GET(self, request): request.transport.socket.settimeout(5) if not getattr(twisted_bluetooth, "SCOReader", None): raise Exception("Not supported in your platform") address = request.path.split("/", 2)[-1].replace("_", ":") if address == "" or len(address) != 17: raise Exception("Invalid address") scoclient = SCOStream(request) scoclient.process() scoclient.target = address if len(address) == 17: if not twisted_bluetooth.SCOConnected(address): log.msg("Connecting SCO to %s" % address) client = twisted_bluetooth.SCOReader(address, self.gotFrame, self.lostConnection) log.msg("SCO connected") return server.NOT_DONE_YET if __name__ == '__main__': from twisted.web.server import Site import sys log.startLogging(sys.stdout) root = Resource() root.isLeaf = False root.putChild("sco", SCOResource()) reactor.listenTCP(8800, Site(root), interface="0.0.0.0") reactor.run() #!/usr/bin/env python
def render_GET(self, request): request.transport.socket.settimeout(5) if not getattr(twisted_bluetooth, "SCOReader", None): raise Exception("Not supported in your platform") address = request.path.split("/",2)[-1].replace("_", ":") if address == "" or len(address) != 17: raise Exception("Invalid address") scoclient = SCOStream(request) scoclient.process() scoclient.target = address if len(address) == 17: if not twisted_bluetooth.SCOConnected(address): log.msg("Connecting SCO to %s" % address) client = twisted_bluetooth.SCOReader(address, self.gotFrame, self.lostConnection) log.msg("SCO connected") return server.NOT_DONE_YET if __name__ == '__main__': from twisted.web.server import Site import sys log.startLogging(sys.stdout) root = Resource() root.isLeaf = False root.putChild("sco", SCOResource()) reactor.listenTCP(8800, Site(root), interface="0.0.0.0") reactor.run()#!/usr/bin/env python