本文整理汇总了Python中twisted.web.http.HTTPFactory方法的典型用法代码示例。如果您正苦于以下问题:Python http.HTTPFactory方法的具体用法?Python http.HTTPFactory怎么用?Python http.HTTPFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.http
的用法示例。
在下文中一共展示了http.HTTPFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def __init__(self, resource, requestFactory=None, *args, **kwargs):
"""
@param resource: The root of the resource hierarchy. All request
traversal for requests received by this factory will begin at this
resource.
@type resource: L{IResource} provider
@param requestFactory: Overwrite for default requestFactory.
@type requestFactory: C{callable} or C{class}.
@see: L{twisted.web.http.HTTPFactory.__init__}
"""
http.HTTPFactory.__init__(self, *args, **kwargs)
self.sessions = {}
self.resource = resource
if requestFactory is not None:
self.requestFactory = requestFactory
示例2: test_nonASCII
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_nonASCII(self):
"""
Bytes in fields of the request which are not part of ASCII are escaped
in the result.
"""
reactor = Clock()
reactor.advance(1234567890)
timestamp = http.datetimeToLogString(reactor.seconds())
request = DummyRequestForLogTest(http.HTTPFactory(reactor=reactor))
request.client = IPv4Address("TCP", b"evil x-forwarded-for \x80", 12345)
request.method = b"POS\x81"
request.protocol = b"HTTP/1.\x82"
request.requestHeaders.addRawHeader(b"referer", b"evil \x83")
request.requestHeaders.addRawHeader(b"user-agent", b"evil \x84")
line = http.combinedLogFormatter(timestamp, request)
self.assertEqual(
u'"evil x-forwarded-for \\x80" - - [13/Feb/2009:23:31:30 +0000] '
u'"POS\\x81 /dummy HTTP/1.0" 123 - "evil \\x83" "evil \\x84"',
line)
示例3: _xforwardedforTest
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def _xforwardedforTest(self, header):
"""
Assert that a request with the given value in its I{X-Forwarded-For}
header is logged by L{proxiedLogFormatter} the same way it would have
been logged by L{combinedLogFormatter} but with 172.16.1.2 as the
client address instead of the normal value.
@param header: An I{X-Forwarded-For} header with left-most address of
172.16.1.2.
"""
reactor = Clock()
reactor.advance(1234567890)
timestamp = http.datetimeToLogString(reactor.seconds())
request = DummyRequestForLogTest(http.HTTPFactory(reactor=reactor))
expected = http.combinedLogFormatter(timestamp, request).replace(
u"1.2.3.4", u"172.16.1.2")
request.requestHeaders.setRawHeaders(b"x-forwarded-for", [header])
line = http.proxiedLogFormatter(timestamp, request)
self.assertEqual(expected, line)
示例4: test_requestBodyDefaultTimeout
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_requestBodyDefaultTimeout(self):
"""
L{HTTPChannel}'s default timeout is 60 seconds.
"""
clock = Clock()
transport = StringTransport()
factory = http.HTTPFactory()
protocol = factory.buildProtocol(None)
# This is a terrible violation of the abstraction later of
# _genericHTTPChannelProtocol, but we need to do it because
# policies.TimeoutMixin doesn't accept a reactor on the object.
# See https://twistedmatrix.com/trac/ticket/8488
protocol._channel.callLater = clock.callLater
protocol.makeConnection(transport)
protocol.dataReceived(b'POST / HTTP/1.0\r\nContent-Length: 2\r\n\r\n')
clock.advance(59)
self.assertFalse(transport.disconnecting)
clock.advance(1)
self.assertTrue(transport.disconnecting)
示例5: test_requestBodyTimeoutFromFactory
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_requestBodyTimeoutFromFactory(self):
"""
L{HTTPChannel} timeouts whenever data from a request body is not
delivered to it in time, even when it gets built from a L{HTTPFactory}.
"""
clock = Clock()
factory = http.HTTPFactory(timeout=100, reactor=clock)
factory.startFactory()
protocol = factory.buildProtocol(None)
transport = StringTransport()
# Confirm that the timeout is what we think it is.
self.assertEqual(protocol.timeOut, 100)
# This is a terrible violation of the abstraction later of
# _genericHTTPChannelProtocol, but we need to do it because
# policies.TimeoutMixin doesn't accept a reactor on the object.
# See https://twistedmatrix.com/trac/ticket/8488
protocol._channel.callLater = clock.callLater
protocol.makeConnection(transport)
protocol.dataReceived(b'POST / HTTP/1.0\r\nContent-Length: 2\r\n\r\n')
clock.advance(99)
self.assertFalse(transport.disconnecting)
clock.advance(2)
self.assertTrue(transport.disconnecting)
示例6: test_clientAddrIPv6
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_clientAddrIPv6(self):
"""
A request from an IPv6 client is logged with that IP address.
"""
reactor = Clock()
reactor.advance(1234567890)
timestamp = http.datetimeToLogString(reactor.seconds())
request = DummyRequestForLogTest(http.HTTPFactory(reactor=reactor))
request.client = IPv6Address("TCP", b"::1", 12345)
line = http.combinedLogFormatter(timestamp, request)
self.assertEqual(
u'"::1" - - [13/Feb/2009:23:31:30 +0000] '
u'"GET /dummy HTTP/1.0" 123 - "-" "-"',
line)
示例7: test_transportNotAbortedWithZeroAbortTimeout
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_transportNotAbortedWithZeroAbortTimeout(self):
"""
If the L{HTTPChannel} has its c{abortTimeout} set to L{None}, it never
aborts.
"""
clock = Clock()
transport = StringTransport()
factory = http.HTTPFactory()
protocol = factory.buildProtocol(None)
protocol._channel.abortTimeout = None
protocol = parametrizeTimeoutMixin(protocol, clock)
protocol.makeConnection(transport)
protocol.dataReceived(b'POST / HTTP/1.0\r\nContent-Length: 2\r\n\r\n')
self.assertFalse(transport.disconnecting)
self.assertFalse(transport.disconnected)
# Force the initial timeout.
clock.advance(60)
self.assertTrue(transport.disconnecting)
self.assertFalse(transport.disconnected)
# Move an absurdly long way just to prove the point.
clock.advance(2**32)
self.assertTrue(transport.disconnecting)
self.assertFalse(transport.disconnected)
示例8: test_connectionLostAfterForceClose
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_connectionLostAfterForceClose(self):
"""
If a timed out transport doesn't close after 15 seconds, the
L{HTTPChannel} will forcibly close it.
"""
clock = Clock()
transport = StringTransport()
factory = http.HTTPFactory()
protocol = factory.buildProtocol(None)
protocol = parametrizeTimeoutMixin(protocol, clock)
protocol.makeConnection(transport)
protocol.dataReceived(b'POST / HTTP/1.0\r\nContent-Length: 2\r\n\r\n')
self.assertFalse(transport.disconnecting)
self.assertFalse(transport.disconnected)
# Force the initial timeout and the follow-on forced closure.
clock.advance(60)
clock.advance(15)
self.assertTrue(transport.disconnecting)
self.assertTrue(transport.disconnected)
# Now call connectionLost on the protocol. This is done by some
# transports, including TCP and TLS. We don't have anything we can
# assert on here: this just must not explode.
protocol.connectionLost(ConnectionDone)
示例9: test_genericHTTPChannelCallLaterUpgrade
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_genericHTTPChannelCallLaterUpgrade(self):
"""
If C{callLater} is patched onto the L{http._GenericHTTPChannelProtocol}
then we need to propagate it across onto a new backing channel after
upgrade.
"""
clock = Clock()
factory = http.HTTPFactory(reactor=clock)
protocol = factory.buildProtocol(None)
self.assertEqual(protocol.callLater, clock.callLater)
self.assertEqual(protocol._channel.callLater, clock.callLater)
transport = StringTransport()
transport.negotiatedProtocol = b'h2'
protocol.requestFactory = DummyHTTPHandler
protocol.makeConnection(transport)
# Send a byte to make it think the handshake is done.
protocol.dataReceived(b'P')
self.assertEqual(protocol.callLater, clock.callLater)
self.assertEqual(protocol._channel.callLater, clock.callLater)
示例10: test_requestBodyTimeoutFromFactory
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_requestBodyTimeoutFromFactory(self):
"""
L{HTTPChannel} timeouts whenever data from a request body is not
delivered to it in time, even when it gets built from a L{HTTPFactory}.
"""
clock = Clock()
factory = http.HTTPFactory(timeout=100, reactor=clock)
factory.startFactory()
protocol = factory.buildProtocol(None)
transport = StringTransport()
protocol = parametrizeTimeoutMixin(protocol, clock)
# Confirm that the timeout is what we think it is.
self.assertEqual(protocol.timeOut, 100)
protocol.makeConnection(transport)
protocol.dataReceived(b'POST / HTTP/1.0\r\nContent-Length: 2\r\n\r\n')
clock.advance(99)
self.assertFalse(transport.disconnecting)
clock.advance(2)
self.assertTrue(transport.disconnecting)
示例11: run
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def run(self):
print 'Starting Thread:' + self.objectName()
listenPort = self.port
spoofFavicon = False
killSessions = True
print 'SSLstrip v0.9 by Moxie Marlinspike Thread::online'
URLMonitor.getInstance().setFaviconSpoofing(spoofFavicon)
CookieCleaner.getInstance().setEnabled(killSessions)
strippingFactory = http.HTTPFactory(timeout=10)
strippingFactory.protocol = StrippingProxy
reactor.listenTCP(int(listenPort), strippingFactory)
reactor.run(installSignalHandlers=False)
示例12: buildProtocol
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def buildProtocol(self, addr):
"""
Generate a channel attached to this site.
"""
channel = http.HTTPFactory.buildProtocol(self, addr)
channel.requestFactory = self.requestFactory
channel.site = self
return channel
示例13: test_RequestRequiringFactorySiteInConstructor
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_RequestRequiringFactorySiteInConstructor(self):
"""
A custom L{Request} subclass that requires the site and factory in the
constructor is able to get them.
"""
d = defer.Deferred()
class SuperRequest(DummyHTTPHandler):
def __init__(self, *args, **kwargs):
DummyHTTPHandler.__init__(self, *args, **kwargs)
d.callback((self.channel.site, self.channel.factory))
connection = H2Connection()
httpFactory = http.HTTPFactory()
connection.requestFactory = SuperRequest
# Create some sentinels to look for.
connection.factory = httpFactory
connection.site = object()
self.connectAndReceive(connection, self.getRequestHeaders, [])
def validateFactoryAndSite(args):
site, factory = args
self.assertIs(site, connection.site)
self.assertIs(factory, connection.factory)
d.addCallback(validateFactoryAndSite)
# We need to wait for the stream cleanup callback to drain the
# response.
cleanupCallback = connection._streamCleanupCallbacks[1]
return defer.gatherResults([d, cleanupCallback])
示例14: test_explicitReactor
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_explicitReactor(self):
"""
L{http.HTTPFactory.__init__} accepts a reactor argument which is set on
L{http.HTTPFactory._reactor}.
"""
reactor = "I am a reactor!"
factory = http.HTTPFactory(reactor=reactor)
self.assertIs(factory._reactor, reactor)
示例15: test_defaultReactor
# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import HTTPFactory [as 别名]
def test_defaultReactor(self):
"""
Giving no reactor argument to L{http.HTTPFactory.__init__} means it
will select the global reactor.
"""
from twisted.internet import reactor
factory = http.HTTPFactory()
self.assertIs(factory._reactor, reactor)