def testSimple(self): r = resource.Resource() r.isLeaf = 0 rr = RootResource() r.putChild(b'foo', rr) rr.putChild(b'', rr) rr.putChild(b'bar', resource.Resource()) chan = self.createServer(r) for url in [b'/foo/', b'/foo/bar', b'/foo/bar/baz', b'/foo/bar/']: request = server.Request(chan, 1) request.setHost(b'example.com', 81) request.gotLength(0) request.requestReceived(b'GET', url, b'HTTP/1.0') self.assertEqual(request.getRootURL(), b"http://example.com/foo")
def __init__(self, message_store, batch_id): resource.Resource.__init__(self) self.message_store = message_store self.batch_id = batch_id inbound = resource.Resource() inbound.putChild('match', MatchResource('inbound', message_store, batch_id)) self.putChild('inbound', inbound) outbound = resource.Resource() outbound.putChild('match', MatchResource('outbound', message_store, batch_id)) self.putChild('outbound', outbound)
def createResourceSite(): # this may seem a bit much, but makes it much simpler to add or change something later top_resource = resource.Resource() nsi_resource = resource.Resource() services_resource = resource.Resource() soap_resource = SOAPResource() top_resource.putChild('NSI', nsi_resource) nsi_resource.putChild('services', services_resource) services_resource.putChild('ConnectionService', soap_resource) site = server.Site(top_resource, logPath='/dev/null') return soap_resource, site
def test_node(self): bitd = uniod() mm_root = resource.Resource() mm_root.putChild('', jsonrpc.HTTPServer(mm_provider)) mm_port = reactor.listenTCP(0, server.Site(mm_root)) n = node.Node(bitd, bitd, [], [], mynet) yield n.start() wb = work.WorkerBridge(node=n, my_pubkey_hash=42, donation_percentage=2, merged_urls=[('http://127.0.0.1:%i' % (mm_port.getHost().port, ), '')], worker_fee=3) web_root = resource.Resource() worker_interface.WorkerInterface(wb).attach_to(web_root) port = reactor.listenTCP(0, server.Site(web_root)) proxy = jsonrpc.HTTPProxy( 'http://127.0.0.1:' + str(port.getHost().port), headers=dict(Authorization='Basic ' + base64.b64encode('user/0:password'))) yield deferral.sleep(3) for i in xrange(100): blah = yield proxy.rpc_getwork() yield proxy.rpc_getwork(blah['data']) yield deferral.sleep(3) assert len(n.tracker.items) == 100 assert n.tracker.verified.get_height(n.best_share_var.value) == 100 wb.stop() n.stop() yield port.stopListening() del n, wb, web_root, port, proxy import gc gc.collect() gc.collect() gc.collect() yield deferral.sleep(20) # waiting for work_poller to exit yield mm_port.stopListening()
def test_defaultReactor(self): """ If not value is passed to L{server.Session.__init__}, the global reactor is used. """ session = server.Session(server.Site(resource.Resource()), b'123') self.assertIdentical(session._reactor, reactor)
def __init__(self, port): from twisted.web import server, resource, distrib root = resource.Resource() self.r = r = distrib.ResourceSubscription("localhost", port) root.putChild('remote', r) self.p = p = reactor.listenTCP(0, server.Site(root)) self.portnum = p.getHost().port
def testDeferredResource(self): r = resource.Resource() r.isLeaf = 1 s = SDResource(r) d = DummyRequest(['foo', 'bar', 'baz']) resource.getChildForRequest(s, d) self.assertEqual(d.postpath, ['bar', 'baz'])
def main(): from twisted.internet import reactor root = resource.Resource() root.putChild('RPC2', XMLRPCQuoter()) root.putChild('SOAP', SOAPQuoter()) reactor.listenTCP(7080, server.Site(root)) reactor.run()
def setUp(self): self.agent = None # for twisted.web.client.Agent test self.cleanupServerConnections = 0 r = resource.Resource() r.putChild(b"file", Data(b"0123456789", "text/html")) r.putChild(b"redirect", Redirect(b"/file")) self.infiniteRedirectResource = CountingRedirect(b"/infiniteRedirect") r.putChild(b"infiniteRedirect", self.infiniteRedirectResource) r.putChild(b"wait", ForeverTakingResource()) r.putChild(b"write-then-wait", ForeverTakingResource(write=True)) r.putChild(b"never-read", ForeverTakingNoReadingResource()) r.putChild(b"error", ErrorResource()) r.putChild(b"nolength", NoLengthResource()) r.putChild(b"host", HostHeaderResource()) r.putChild(b"payload", PayloadResource()) r.putChild(b"broken", BrokenDownloadResource()) r.putChild(b"cookiemirror", CookieMirrorResource()) r.putChild(b'delay1', DelayResource(1)) r.putChild(b'delay2', DelayResource(2)) self.afterFoundGetCounter = CountingResource() r.putChild(b"afterFoundGetCounter", self.afterFoundGetCounter) r.putChild(b"afterFoundGetRedirect", Redirect(b"/afterFoundGetCounter")) miscasedHead = Data(b"miscased-head GET response content", "major/minor") miscasedHead.render_Head = lambda request: b"miscased-head content" r.putChild(b"miscased-head", miscasedHead) self.extendedRedirect = ExtendedRedirect(b'/extendedRedirect') r.putChild(b"extendedRedirect", self.extendedRedirect) self.site = server.Site(r, timeout=None) self.wrapper = WrappingFactory(self.site) self.port = self._listen(self.wrapper) self.portno = self.port.getHost().port
def create_status_service(proxy_server, port): """Create the status service.""" root = resource.Resource() root.putChild('status', _Status(proxy_server)) site = server.Site(root) service = TCPServer(port, site) return service
def run_server(reactor, o): print("dir is", __file__) provroot = static.File(htmldir) provisioner = Provisioner(reactor, o) provroot.putChild(b"", provisioner) provroot.putChild(b"index.html", provisioner) provroot.putChild(b"request-code", RequestCode(reactor, o)) # Prefix the mountpoints. revpaths = o['mountpoint'].split('/') revpaths.reverse() for dir in revpaths: # print('mount root under ' + dir) if dir != '': r = resource.Resource() r.putChild(dir.encode('utf-8'), provroot) provroot = r # Override the paths. root = StackedResource([static.File(wwwroot(o['home'])), provroot]) if o['mountpoint'] == '/': root.putChild(b"", provisioner) # Display the JSON config. root.putChild(b"network-config", ConfigJSON(o)) root.putChild(b"genesis.json", GenesisJSON(o)) site = server.Site(root) s = endpoints.serverFromString(reactor, o["listen"]) s.listen(site) print("server running") return defer.Deferred()
def setUp(self): self.gw = _twisted.TwistedGateway(expose_request=False) root = resource.Resource() root.putChild('', self.gw) self.p = reactor.listenTCP(0, server.Site(root), interface="127.0.0.1") self.port = self.p.getHost().port
def makeService(options): """ This will be called from twistd plugin system and we are supposed to create and return our application service. """ application = service.IServiceCollection( service.Application("octopus_server", uid=1, gid=1)) wamp_service = WampService(int(options["wampport"]), debug=False) wamp_service.setServiceParent(application) ExperimentMarshal.publish = wamp_service.factory.dispatch internet.TCPServer(int(options["pbport"]), pb.PBServerFactory( ExperimentMarshal())).setServiceParent(application) resources_path = os.path.join(os.path.dirname(__file__), "resources") # class ExperimentListRealm (object): # """ # A realm which gives out L{ExperimentList} instances for authenticated # users. # """ # implements(IRealm) # def requestAvatar(self, avatarId, mind, *interfaces): # if resource.IResource in interfaces: # return resource.IResource, ExperimentList(), lambda: None # raise NotImplementedError() #from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse #checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe='blow')] #wrapper = guard.HTTPAuthSessionWrapper( # Portal(ExperimentListRealm(), checkers), # [guard.DigestCredentialFactory('md5', 'example.com')]) # Read in preconfigured scripts list if options["scripts"] is not None: try: with open(options["scripts"]) as f: scripts = [line.split("\t") for line in f.readlines()] scripts = [(s[0], s[1].strip()) for s in scripts if len(s) is 2] except IOError: scripts = [] else: scripts = [] root = resource.Resource() root.putChild("", Root()) #root.putChild("experiments", wrapper) root.putChild("experiments", ExperimentList()) root.putChild("programs", ProgramList(scripts)) root.putChild("resources", static.File(resources_path)) site = server.Site(root) internet.TCPServer(int(options["port"]), site).setServiceParent(application) return application
def on_load(self): self.config = self.server.config.web with open('./web/js/init.js', 'w') as f: port = self.config.web_interface_port2 auth = self.config.auth_key f.write('var server_port = "%s";\n var auth_key = "%s"' % (port, auth)) root = File('./web') root.indexNames = ['index.html'] root.putChild('css', static.File("./web/css")) root.putChild('js', static.File("./web/js")) root.putChild('img', static.File("./web/img")) checker = PasswordDictChecker(self.config) realm = HttpPasswordRealm(root) p = portal.Portal(realm, [checker]) credentialFactory = DigestCredentialFactory("md5", "Cuwo Interface Login") protected_resource = HTTPAuthSessionWrapper(p, [credentialFactory]) auth_resource = resource.Resource() auth_resource.putChild("", protected_resource) site = SiteOverride(auth_resource) reactor.listenTCP(self.config.web_interface_port1, site) self.web_factory = WebFactory(self) reactor.listenTCP(self.config.web_interface_port2, WebSocketFactory(self.web_factory))
def start_api(self): root = resource.Resource() root.putChild('', self) self.port = reactor.listenTCP(self.rpc_port, Site(root), interface='localhost') log.info("started jsonrpc server")
def startServer(self, cgi): root = resource.Resource() cgipath = util.sibpath(__file__, cgi) root.putChild("cgi", PythonScript(cgipath)) site = server.Site(root) self.p = reactor.listenTCP(0, site) return self.p.getHost().port
def testDeferredResource(self): r = resource.Resource() r.isLeaf = 1 s = SDResource(r) d = DummyRequest(["foo", "bar", "baz"]) resource.getChildForRequest(s, d) self.assertEqual(d.postpath, ["bar", "baz"])
def start_httpserver(options, protocol=vtk_wamp.ServerProtocol, disableLogging=False): """ Starts an http-only server with the given protocol. The options argument should contain 'host', 'port', 'urlRegex', and optionally 'content'. """ from twisted.web import server, http from twisted.internet import reactor from twisted.web.static import File host = options.host port = options.port contentDir = options.content rootPath = options.hp # Initialize web resource web_resource = File(contentDir) if contentDir else resource.Resource() # Add the rpc method server handle_complex_resource_path( rootPath, web_resource, vtk_wamp.HttpRpcResource(protocol(None), rootPath)) site = server.Site(web_resource) reactor.listenTCP(port, site, interface=host) reactor.run()
def makeService(self, options): root = resource.Resource() fname = pkg_resources.resource_filename("static_server", "a_file.html") static_resource = static.File(fname) root.putChild('', static_resource) site = server.Site(root) return strports.service('tcp:8000', site)
def start(self): self.announced_startup = True root = resource.Resource() root.putChild('', self) self.port = reactor.listenTCP(self.rpc_port, Site(root), interface='localhost') log.info("starting %i nodes on %s, rpc available on localhost:%i", len(self._nodes), self.external_ip, self.rpc_port) for node in self._nodes: node.start_listening() yield node._protocol._listening for node1 in self._nodes: for node2 in self._nodes: if node1 is node2: continue try: yield node1.addContact( node1.contact_manager.make_contact( node2.node_id, node2.externalIP, node2.port, node1._protocol)) except TransportNotConnected: pass node1.safe_start_looping_call(node1._change_token_lc, constants.tokenSecretChangeInterval) node1.safe_start_looping_call(node1._refresh_node_lc, constants.checkRefreshInterval) node1._join_deferred = defer.succeed(True) reactor.addSystemEventTrigger('before', 'shutdown', self.stop) log.info("finished bootstrapping the network, running %i nodes", len(self._nodes))
class DummyChannel: class TCP: port = 80 def __init__(self): self.written = StringIO() self.producers = [] def getPeer(self): return IPv4Address("TCP", '192.168.1.1', 12344) def write(self, bytes): assert isinstance(bytes, str) self.written.write(bytes) def writeSequence(self, iovec): map(self.write, iovec) def getHost(self): return IPv4Address("TCP", '10.0.0.1', self.port) def registerProducer(self, producer, streaming): self.producers.append((producer, streaming)) class SSL(TCP): implements(interfaces.ISSLTransport) site = server.Site(resource.Resource()) def __init__(self): self.transport = self.TCP() def requestDone(self, request): pass
def setup_site(port): root = resource.Resource() static_files = static.File(os.path.join(os.path.dirname(__file__), 'static')) root.putChild('static', static_files) site = server.Site(root) root.putChild('tcp', TCPResource(port)) return site
def __init__(self, info, options): # create an empty root for our site root = resource.Resource() # serve our static content root.putChild('static', static.File(os.path.join(info.productDir, 'web'))) # serve our dynamic content dynamic = resource.Resource() root.putChild('archiver', dynamic) import archiver.browse dynamic.putChild('browse', archiver.browse.BrowseHandler('browse', options)) dynamic.putChild('info', InfoHandler('info', info, options)) #dynamic.putChild('log',LogHandler('log')) # create the site we will serve FilteredSite.__init__(self, root)
def main(args): parser = ArgumentParser() parser.add_argument("-p", "--port", default=8080, type=int, help="Port to listen on.") args = parser.parse_args() WEBMUX_STATIC_PATH = os.path.join(webmux.__path__[0], "static") root = Home() static_path = resource.Resource() static_path.putChild("css", static.File(WEBMUX_STATIC_PATH + "/css")) static_path.putChild("js", static.File(WEBMUX_STATIC_PATH + "/js")) static_path.putChild("img", static.File(WEBMUX_STATIC_PATH + "/img")) root.putChild("terminal", SockJSResource(TerminalFactory)) root.putChild("signup", Signup()) root.putChild("login", Login()) root.putChild("logout", Logout()) root.putChild("static", static_path) site = server.Site(root) site.sessionFactory = LongSession reactor.listenTCP(args.port, site) reactor.callLater(0, init) reactor.run()
def _setupDistribServer(self, child): """ Set up a resource on a distrib site using L{ResourcePublisher}. @param child: The resource to publish using distrib. @return: A tuple consisting of the host and port on which to contact the created site. """ distribRoot = resource.Resource() distribRoot.putChild("child", child) distribSite = server.Site(distribRoot) self.f1 = distribFactory = PBServerFactory( distrib.ResourcePublisher(distribSite)) distribPort = reactor.listenTCP( 0, distribFactory, interface="127.0.0.1") self.addCleanup(distribPort.stopListening) addr = distribPort.getHost() self.sub = mainRoot = distrib.ResourceSubscription( addr.host, addr.port) mainSite = server.Site(mainRoot) mainPort = reactor.listenTCP(0, mainSite, interface="127.0.0.1") self.addCleanup(mainPort.stopListening) mainAddr = mainPort.getHost() return mainPort, mainAddr
def test_getSessionNonExistent(self): """ L{site.getSession} raises a L{KeyError} if the session is not found. """ site = server.Site(resource.Resource()) self.assertRaises(KeyError, site.getSession, b'no-such-uid')
def start(cls, net, factory, uniod, peer_ports, merged_urls): self = cls() self.n = node.Node(factory, uniod, [], [], net) yield self.n.start() self.n.p2p_node = node.P2PNode(self.n, port=0, max_incoming_conns=1000000, addr_store={}, connect_addrs=[ ('127.0.0.1', peer_port) for peer_port in peer_ports ]) self.n.p2p_node.start() wb = work.WorkerBridge(node=self.n, my_pubkey_hash=random.randrange(2**160), donation_percentage=random.uniform(0, 10), merged_urls=merged_urls, worker_fee=3) self.wb = wb web_root = resource.Resource() worker_interface.WorkerInterface(wb).attach_to(web_root) self.web_port = reactor.listenTCP(0, server.Site(web_root)) defer.returnValue(self)
def test_noBytesResult(self): """ When implemented C{render} method does not return bytes an internal server error is returned. """ class RiggedRepr(object): def __repr__(self): return 'my>repr' result = RiggedRepr() no_bytes_resource = resource.Resource() no_bytes_resource.render = lambda request: result request = self._getReq(no_bytes_resource) request.requestReceived(b"GET", b"/newrender", b"HTTP/1.0") body = request._transport.getvalue() self.assertEqual(request.code, 500) expected = [ '', '<html>', ' <head><title>500 - Request did not return bytes</title></head>', ' <body>', ' <h1>Request did not return bytes</h1>', ' <p>Request: <pre><%s></pre><br />' 'Resource: <pre><%s></pre><br />' 'Value: <pre>my>repr</pre></p>' % ( reflect.safe_repr(request)[1:-1], reflect.safe_repr(no_bytes_resource)[1:-1], ), ' </body>', '</html>', '' ] self.assertEqual('\n'.join(expected).encode('ascii'), body)
def setupSOAPResource(top_resource, resource_name, subpath=None, allowed_hosts=None): # Default path: NSI/services/{resource_name} if subpath is None: subpath = ['NSI', 'services'] ir = top_resource for path in subpath: if path in ir.children: ir = ir.children[path] else: nr = resource.Resource() ir.putChild(path, nr) ir = nr if resource_name in ir.children: raise AssertionError, 'Trying to insert several SOAP resource in same leaf. Go away.' soap_resource = SOAPResource(allowed_hosts=allowed_hosts) ir.putChild(resource_name, soap_resource) return soap_resource
def _requestTest(self, child, **kwargs): """ Set up a resource on a distrib site using L{ResourcePublisher} and then retrieve it from a L{ResourceSubscription} via an HTTP client. @param child: The resource to publish using distrib. @param **kwargs: Extra keyword arguments to pass to L{getPage} when requesting the resource. @return: A L{Deferred} which fires with the result of the request. """ distribRoot = resource.Resource() distribRoot.putChild("child", child) distribSite = server.Site(distribRoot) self.f1 = distribFactory = PBServerFactory( distrib.ResourcePublisher(distribSite)) distribPort = reactor.listenTCP(0, distribFactory, interface="127.0.0.1") self.addCleanup(distribPort.stopListening) addr = distribPort.getHost() self.sub = mainRoot = distrib.ResourceSubscription( addr.host, addr.port) mainSite = server.Site(mainRoot) mainPort = reactor.listenTCP(0, mainSite, interface="127.0.0.1") self.addCleanup(mainPort.stopListening) mainAddr = mainPort.getHost() return client.getPage( "http://%s:%s/child" % (mainAddr.host, mainAddr.port), **kwargs)