本文整理汇总了Python中twisted.web.proxy.ProxyClient.dataReceived方法的典型用法代码示例。如果您正苦于以下问题:Python ProxyClient.dataReceived方法的具体用法?Python ProxyClient.dataReceived怎么用?Python ProxyClient.dataReceived使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.proxy.ProxyClient
的用法示例。
在下文中一共展示了ProxyClient.dataReceived方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _testDataForward
# 需要导入模块: from twisted.web.proxy import ProxyClient [as 别名]
# 或者: from twisted.web.proxy.ProxyClient import dataReceived [as 别名]
def _testDataForward(self, data, method="GET", body=""):
"""
Build a fake proxy connection, and send C{data} over it, checking that
it's forwarded to the originating request.
"""
# Connect everything
clientTransport = StringTransportWithDisconnection()
serverTransport = StringTransportWithDisconnection()
channel = DummyChannel(serverTransport)
parent = DummyParent(channel)
serverTransport.protocol = channel
client = ProxyClient(method, '/foo', 'HTTP/1.0',
{"accept": "text/html"}, body, parent)
clientTransport.protocol = client
client.makeConnection(clientTransport)
# Check data sent
self.assertEquals(clientTransport.value(),
"%s /foo HTTP/1.0\r\n"
"connection: close\r\n"
"accept: text/html\r\n\r\n%s" % (method, body))
# Fake an answer
client.dataReceived(data)
# Check that the data has been forwarded
self.assertEquals(serverTransport.value(), data)
clientTransport.loseConnection()
self.assertIsInstance(channel.lostReason, ConnectionDone)
示例2: _testDataForward
# 需要导入模块: from twisted.web.proxy import ProxyClient [as 别名]
# 或者: from twisted.web.proxy.ProxyClient import dataReceived [as 别名]
def _testDataForward(self, code, message, headers, body, method="GET",
requestBody="", loseConnection=True):
"""
Build a fake proxy connection, and send C{data} over it, checking that
it's forwarded to the originating request.
"""
request = DummyRequest(['foo'])
# Connect a proxy client to a fake transport.
clientTransport = StringTransportWithDisconnection()
client = ProxyClient(method, '/foo', 'HTTP/1.0',
{"accept": "text/html"}, requestBody, request)
clientTransport.protocol = client
client.makeConnection(clientTransport)
# Check data sent
self.assertEquals(clientTransport.value(),
"%s /foo HTTP/1.0\r\n"
"connection: close\r\n"
"accept: text/html\r\n\r\n%s" % (method, requestBody))
# Fake an answer
client.dataReceived("HTTP/1.0 %d %s\r\n" % (code, message))
for (header, values) in headers:
for value in values:
client.dataReceived("%s: %s\r\n" % (header, value))
client.dataReceived("\r\n" + body)
# Check that the response data has been forwarded back to the original
# requester.
self.assertEquals(request.responseCode, code)
self.assertEquals(request.responseMessage, message)
receivedHeaders = list(request.responseHeaders.getAllRawHeaders())
receivedHeaders.sort()
expectedHeaders = headers[:]
expectedHeaders.sort()
self.assertEquals(receivedHeaders, expectedHeaders)
self.assertEquals(''.join(request.written), body)
# Check that when the response is done, the request is finished.
if loseConnection:
clientTransport.loseConnection()
# Even if we didn't call loseConnection, the transport should be
# disconnected. This lets us not rely on the server to close our
# sockets for us.
self.assertFalse(clientTransport.connected)
self.assertEquals(request.finished, 1)