本文整理汇总了Python中twisted.web.server.Site.buildProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python Site.buildProtocol方法的具体用法?Python Site.buildProtocol怎么用?Python Site.buildProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.server.Site
的用法示例。
在下文中一共展示了Site.buildProtocol方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PappyWebServer
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
class PappyWebServer(object):
"""
A class that is used to serve pages for requests to http://pappy. It is a
ghetto wrapper around a twisted web Site object. Give it a request object
and it will add a response to it.
NOINDEX
"""
from pappyproxy.pappy import session
site_dir = session.config.pappy_dir+'/site'
loader = FileSystemLoader(site_dir)
env = Environment(loader=loader)
def __init__(self):
root = RootResource(self.site_dir)
self.site = Site(root)
@staticmethod
def render_template(*args, **kwargs):
return PappyWebServer.env.get_template(args[0]).render(args[1:], **kwargs).encode('utf-8')
@defer.inlineCallbacks
def handle_request(self, req):
protocol = self.site.buildProtocol(None)
tr = PappyStringTransport()
protocol.makeConnection(tr)
protocol.dataReceived(req.full_request)
tr.waitForProducers()
## WORKING HERE
# use loading functions to load response
yield tr.complete_deferred
rsp_raw = tr.value()
rsp = Response(rsp_raw)
req.response = rsp
示例2: _testRender
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def _testRender(self, uri, expectedURI):
"""
Check that a request pointing at C{uri} produce a new proxy connection,
with the path of this request pointing at C{expectedURI}.
"""
root = Resource()
reactor = MemoryReactor()
resource = ReverseProxyResource("127.0.0.1", 1234, "/path", reactor)
root.putChild("index", resource)
site = Site(root)
transport = StringTransportWithDisconnection()
channel = site.buildProtocol(None)
channel.makeConnection(transport)
# Clear the timeout if the tests failed
self.addCleanup(channel.connectionLost, None)
channel.dataReceived("GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" % (uri,))
# Check that one connection has been created, to the good host/port
self.assertEquals(len(reactor.tcpClients), 1)
self.assertEquals(reactor.tcpClients[0][0], "127.0.0.1")
self.assertEquals(reactor.tcpClients[0][1], 1234)
# Check the factory passed to the connect, and its given path
factory = reactor.tcpClients[0][2]
self.assertIsInstance(factory, ProxyClientFactory)
self.assertEquals(factory.rest, expectedURI)
self.assertEquals(factory.headers["host"], "127.0.0.1:1234")
示例3: buildProtocol
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def buildProtocol(self, addr):
if self._active and len(self._connections)>self.maxConnections:
self.lport.stopReading()
_log.info('Throttling with %s/%s connections',len(self._connections), self.maxConnections )
self._active = False
proto = Site.buildProtocol(self, addr)
if proto:
self._connections[proto] = weakref.ref(proto, self._dec)
_log.debug('build %s %s', addr, proto)
return proto
示例4: buildProtocol
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def buildProtocol(self, addr):
if addr.host == self.ip:
return Site.buildProtocol(self, addr)
return None
示例5: buildProtocol
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def buildProtocol(self, addr):
print addr.host
if addr.host in ALLOWED_IPS:
return Site.buildProtocol(self, addr)
else:
return None
示例6: setUp
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def setUp(self):
super(SiteTest, self).setUp()
factory = Site(ServerRoot())
self.proto = factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransport()
self.proto.makeConnection(self.tr)
示例7: request
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def request(self, method, uri, headers=None, bodyProducer=None):
"""
Implement IAgent.request.
"""
# We want to use Agent to parse the HTTP response, so let's ask it to
# make a request against our in-memory reactor.
response = self._realAgent.request(method, uri, headers, bodyProducer)
# If the request has already finished, just propagate the result. In
# reality this would only happen in failure, but if the agent ever adds
# a local cache this might be a success.
already_called = []
def check_already_called(r):
already_called.append(r)
return r
response.addBoth(check_already_called)
if already_called:
return response
# That will try to establish an HTTP connection with the reactor's
# connectTCP method, and MemoryReactor will place Agent's factory into
# the tcpClients list. Alternately, it will try to establish an HTTPS
# connection with the reactor's connectSSL method, and MemoryReactor
# will place it into the sslClients list. We'll extract that.
if PY3:
scheme = URLPath.fromBytes(uri).scheme
else:
scheme = URLPath.fromString(uri).scheme
host, port, factory, timeout, bindAddress = (
self._memoryReactor.tcpClients[-1])
serverAddress = IPv4Address('TCP', '127.0.0.1', port)
clientAddress = IPv4Address('TCP', '127.0.0.1', 31337)
# Create the protocol and fake transport for the client and server,
# using the factory that was passed to the MemoryReactor for the
# client, and a Site around our rootResource for the server.
serverFactory = Site(self._rootResource, reactor=self._memoryReactor)
serverProtocol = serverFactory.buildProtocol(clientAddress)
serverTransport = iosim.FakeTransport(
serverProtocol, isServer=True,
hostAddress=serverAddress, peerAddress=clientAddress)
clientProtocol = factory.buildProtocol(None)
clientTransport = iosim.FakeTransport(
clientProtocol, isServer=False,
hostAddress=clientAddress, peerAddress=serverAddress)
if scheme == b"https":
# Provide ISSLTransport on both transports, so everyone knows that
# this is HTTPS.
directlyProvides(serverTransport, ISSLTransport)
directlyProvides(clientTransport, ISSLTransport)
# Make a pump for wiring the client and server together.
pump = iosim.connect(
serverProtocol, serverTransport, clientProtocol, clientTransport)
self._pumps.add(pump)
return response
示例8: buildProtocol
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def buildProtocol(self, addr):
if addr.host != self.only_ip and self.only_ip != "0.0.0.0":
return
return Site.buildProtocol(self, addr)
示例9: buildProtocol
# 需要导入模块: from twisted.web.server import Site [as 别名]
# 或者: from twisted.web.server.Site import buildProtocol [as 别名]
def buildProtocol(self, addr):
log.debug(u'Building protocol for {}'.format(addr))
return Site.buildProtocol(self, addr)