示例#1
0
    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
示例#2
0
    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)
示例#3
0
 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
示例#4
0
    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()
示例#5
0
 def testDistrib(self):
     # site1 is the publisher
     r1 = resource.Resource()
     r1.putChild("there", static.Data("root", "text/plain"))
     site1 = server.Site(r1)
     self.f1 = PBServerFactory(distrib.ResourcePublisher(site1))
     self.port1 = reactor.listenTCP(0, self.f1)
     self.sub = distrib.ResourceSubscription("127.0.0.1",
                                             self.port1.getHost().port)
     r2 = resource.Resource()
     r2.putChild("here", self.sub)
     f2 = MySite(r2)
     self.port2 = reactor.listenTCP(0, f2)
     d = client.getPage("http://127.0.0.1:%d/here/there" % \
                        self.port2.getHost().port)
     d.addCallback(self.assertEqual, 'root')
     return d
示例#6
0
    def test_connectionLost(self):
        """
        If there is an error issuing the request to the remote publisher, an
        error response is returned.
        """
        # Using pb.Root as a publisher will cause request calls to fail with an
        # error every time.  Just what we want to test.
        self.f1 = serverFactory = PBServerFactory(pb.Root())
        self.port1 = serverPort = reactor.listenTCP(0, serverFactory)

        self.sub = subscription = distrib.ResourceSubscription(
            "127.0.0.1",
            serverPort.getHost().port)
        request = DummyRequest([b""])
        d = _render(subscription, request)

        def cbRendered(ignored):
            self.assertEqual(request.responseCode, 500)
            # This is the error we caused the request to fail with.  It should
            # have been logged.
            errors = self.flushLoggedErrors(pb.NoSuchMethod)
            self.assertEqual(len(errors), 1)
            # The error page is rendered as HTML.
            expected = [
                b"",
                b"<html>",
                b"  <head><title>500 - Server Connection Lost</title></head>",
                b"  <body>",
                b"    <h1>Server Connection Lost</h1>",
                b"    <p>Connection to distributed server lost:"
                b"<pre>"
                b"[Failure instance: Traceback from remote host -- "
                b"twisted.spread.flavors.NoSuchMethod: "
                b"No such method: remote_request",
                b"]</pre></p>",
                b"  </body>",
                b"</html>",
                b"",
            ]
            self.assertEqual([b"\n".join(expected)], request.written)

        d.addCallback(cbRendered)
        return d
示例#7
0
 def testDistrib(self):
     # site1 is the publisher
     r1 = resource.Resource()
     r1.putChild(b"there", static.Data(b"root", "text/plain"))
     site1 = server.Site(r1)
     self.f1 = PBServerFactory(distrib.ResourcePublisher(site1))
     self.port1 = reactor.listenTCP(0, self.f1)
     self.sub = distrib.ResourceSubscription("127.0.0.1", self.port1.getHost().port)
     r2 = resource.Resource()
     r2.putChild(b"here", self.sub)
     f2 = MySite(r2)
     self.port2 = reactor.listenTCP(0, f2)
     agent = client.Agent(reactor)
     url = f"http://127.0.0.1:{self.port2.getHost().port}/here/there"
     url = url.encode("ascii")
     d = agent.request(b"GET", url)
     d.addCallback(client.readBody)
     d.addCallback(self.assertEqual, b"root")
     return d
示例#8
0
    def test_connectionLost(self):
        """
        If there is an error issuing the request to the remote publisher, an
        error response is returned.
        """
        # Using pb.Root as a publisher will cause request calls to fail with an
        # error every time.  Just what we want to test.
        self.f1 = serverFactory = PBServerFactory(pb.Root())
        self.port1 = serverPort = reactor.listenTCP(0, serverFactory)

        self.sub = subscription = distrib.ResourceSubscription(
            "127.0.0.1", serverPort.getHost().port)
        request = DummyRequest([''])
        d = _render(subscription, request)
        def cbRendered(ignored):
            self.assertEqual(request.responseCode, 500)
            # This is the error we caused the request to fail with.  It should
            # have been logged.
            self.assertEqual(len(self.flushLoggedErrors(pb.NoSuchMethod)), 1)
        d.addCallback(cbRendered)
        return d
示例#9
0
    def test_requestHeaders(self):
        """
        The request headers are available on the request object passed to a
        distributed resource's C{render} method.
        """
        requestHeaders = {}

        class ReportRequestHeaders(resource.Resource):
            def render(self, request):
                requestHeaders.update(
                    dict(request.requestHeaders.getAllRawHeaders()))
                return ""

        distribRoot = resource.Resource()
        distribRoot.putChild("headers", ReportRequestHeaders())
        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()

        request = client.getPage("http://%s:%s/headers" %
                                 (mainAddr.host, mainAddr.port),
                                 headers={'foo': 'bar'})

        def cbRequested(result):
            self.assertEquals(requestHeaders['Foo'], ['bar'])

        request.addCallback(cbRequested)
        return request
示例#10
0
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
This shows an example of a bare-bones distributed web set up.  The "master" and
"slave" parts will usually be in different files -- they are here together only
for brevity of illustration.  In normal usage they would each run in a separate
process.

Usage:
    $ python silly-web.py

Then visit http://localhost:19988/.
"""

from twisted.internet import reactor, protocol
from twisted.web import server, distrib, static
from twisted.spread import pb

# The "master" server
site = server.Site(distrib.ResourceSubscription('unix', '.rp'))
reactor.listenTCP(19988, site)

# The "slave" server
fact = pb.PBServerFactory(distrib.ResourcePublisher(server.Site(static.File('static'))))

reactor.listenUNIX('./.rp', fact)
reactor.run()
示例#11
0
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
This shows an example of a bare-bones distributed web set up.  The "master" and
"slave" parts will usually be in different files -- they are here together only
for brevity of illustration.  In normal usage they would each run in a separate
process.

Usage:
    $ python silly-web.py

Then visit http://localhost:19988/.
"""

from twisted.internet import reactor
from twisted.web import server, distrib, static
from twisted.spread import pb

# The "master" server
site = server.Site(distrib.ResourceSubscription("unix", ".rp"))
reactor.listenTCP(19988, site)

# The "slave" server
fact = pb.PBServerFactory(
    distrib.ResourcePublisher(server.Site(static.File("static"))))

reactor.listenUNIX("./.rp", fact)
reactor.run()