def getChild(self, path, request): if path == 'update_url': print 'Updating url', request.getRequestHostname( ), 'to', request.uri[12:] MyProxyResource.hostMap[ request.getRequestHostname()] = request.uri[12:] resource = static.Data( "<html><body>Mapped: %s to %s</body></html>" % (request.getRequestHostname(), MyProxyResource.hostMap[request.getRequestHostname()]), "text/html") resource.isLeaf = True return resource if path == 'update_uri': host = request.uri[12:].split('..')[0] uri = request.uri[12:].split('..')[1] print 'Updating uri', host, 'to', uri MyProxyResource.hostMap[host] = uri resource = static.Data( "<html><body>Mapped: %s to %s</body></html>" % (host, MyProxyResource.hostMap[host]), "text/html") resource.isLeaf = True return resource return MyProxyResource(self.path + '/' + urlquote(path, safe=""), self.reactor)
def start(self, options): root = static.Data("placeholder", "text/plain") welcome = Welcome(self, options["timestamps"]) root.putChild("", welcome) root.putChild("welcome", welcome) # we used to only do this root.putChild("reload", Reload(self)) root.putChild("all-events", EventView(self)) root.putChild("summary", Summary(self)) root.putChild("flog.css", static.Data(FLOG_CSS, "text/css")) s = server.Site(root) (portnum, interface) = parse_strport(options['port']) self.serv = internet.TCPServer(portnum, s, interface=interface) self.serv.startService() portnum = self.serv._port.getHost().port # TODO: this makes all sort of assumptions: HTTP-vs-HTTPS, localhost. url = "http://localhost:%d/" % portnum if not options["quiet"]: print "scanning.." self.logfiles = [options.dumpfile] self.load_logfiles() if not options["quiet"]: print "please point your browser at:" print url if options["open"]: import webbrowser webbrowser.open(url) return url # for tests
def make_home_page(self): from .. import __version__ as VERSION s = StringIO() s.write("""<html lang="en"><head> <title>CloudMailing API</title> <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head>""") s.write( '<body><div class="container"><div class="page-header"><h1>CloudMailing API services <small>Version %s</small></h1></div>' % VERSION) if self.sub_services: for name, rpc in self.sub_services: s.write('''<div class="row"> <div class="col-md-2 col-md-offset-1"><h2><a href="%(name)s">%(name)s</a></h2></div> <div class="col-md-8"><div class="well">%(description)s</div></div> </div> ''' % { 'name': name, 'description': inspect.getdoc(rpc) }) else: s.write('<style>.red { color: #FF0000; }</style>') s.write('<h2 class="red">Invalid License.</h2>') s.write("</div></body></html>") home = static.Data(s.getvalue(), "text/html") self.putChild("", home)
def do_the_render(self, request): self.warning('do_the_render, %s, %s, %s %r %s', request.method, request.path, request.uri, request.args, request.client) msg = "Houston, we've got a problem" path = request.path.split('/') path = path[2:] self.warning('path %r', path) if request.method in ('GET', 'POST'): request.postpath = None if request.method == 'GET': if path[0] == 'devices': return self.list_devices(request) else: device = self.controlpoint.get_device_with_id(path[0]) if device is not None: service = device.get_service_by_type(path[1]) if service is not None: action = service.get_action(path[2]) if action is not None: return self.call_action(action, request) else: msg = "action %r on service type %r for device %r not found" % (path[2], path[1], path[0]) else: msg = "service type %r for device %r not found" % (path[1], path[0]) else: msg = "device with id %r not found" % path[0] request.setResponseCode(404, message=msg) return static.Data("<html><p>%s</p></html>" % msg, 'text/html')
def testDistrib(self): # site1 is the publisher r1 = resource.Resource() r1.putChild("there", static.Data("root", "text/plain")) site1 = server.Site(r1) f1 = pb.PBServerFactory(distrib.ResourcePublisher(site1)) self.port1 = reactor.listenTCP(0, f1) util.spinUntil(lambda: self.port1.connected) # site2 is the subscriber sub = distrib.ResourceSubscription("127.0.0.1", self.port1.getHost().port) r2 = resource.Resource() r2.putChild("here", sub) f2 = MySite(r2) self.port2 = reactor.listenTCP(0, f2) util.spinUntil(lambda: self.port2.connected) # then we hit site2 with a client d = client.getPage("http://127.0.0.1:%d/here/there" % \ self.port2.getHost().port) res = util.wait(d, timeout=1.0) self.failUnlessEqual(res, "root") # A bit of a hack: force the pb client to disconnect, for cleanup # purposes. sub.publisher.broker.transport.loseConnection()
def __init__(self, descriptions): self.children[''] = RootResource(self.blankImage[0]) with vroot.openPackageFile(self.blankImage[0]) as fh: imageData = fh.read() self.children[self.blankImage[0]] = static.Data( imageData, self.blankImage[1]) self.children['image.png'] = ImageResource(descriptions)
def getData(path, request): user = self.users.get(path, b"No such users <p/> usage: site/user") path = path.decode("ascii") user = user.decode("ascii") text = '<h1>{}</h1><p>{}</p>'.format(path, user) text = text.encode("ascii") return static.Data(text, 'text/html')
def __init__(self, config, broker): resource.Resource.__init__(self) self.broker = broker self.channels = {} self.putChild('', static.Data(self.intro, 'text/plain')) self.putChild('validate', BaseRequest(self, self.validate)) self.putChild('login', BaseRequest(self, self.login))
def __init__(self, app, template_paths=None, cache_size=50, loader=None): resource.Resource.__init__(self) # register log file if any if (app.development is False and app.already_logging is False and app.log_file is not None): log.startLogging(DailyLogFile.fromFullPath(app.log_file)) self._assets = resource.Assets([os.getcwd() + '/static']) self.template_paths = [ 'application/view/templates', '{}/templates/jinja'.format( os.path.dirname(__file__).rsplit(os.sep, 1)[0]) ] # set managers self._controllers_manager = app.managers.get('controller') self._shared_controllers_manager = app.managers.get('packages') # register controllers self.register_shared_controllers() self.register_controllers() # containers self.containers = { 'styles': static.Data('', 'text/css'), 'scripts': static.Data('', 'text/javascript') } # register containers self.putChild('styles', self.containers['styles']) self.putChild('scripts', self.containers['scripts']) # insert stylesheets and scripts self.insert_stylesheets() self.insert_scripts() # register service ponger self.putChild('_mamba_pong', static.Data('PONG', 'text/plain')) # static accessible data (scripts, css, images, and others) self.putChild('assets', self._assets) # other initializations self.generate_dispatches() self.initialize_templating_system(template_paths, cache_size, loader)