本文整理汇总了Python中proxy.Proxy._process_request方法的典型用法代码示例。如果您正苦于以下问题:Python Proxy._process_request方法的具体用法?Python Proxy._process_request怎么用?Python Proxy._process_request使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类proxy.Proxy
的用法示例。
在下文中一共展示了Proxy._process_request方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestProxy
# 需要导入模块: from proxy import Proxy [as 别名]
# 或者: from proxy.Proxy import _process_request [as 别名]
class TestProxy(unittest.TestCase):
http_server = None
http_server_port = None
http_server_thread = None
@staticmethod
def get_available_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.bind(('', 0))
_, port = sock.getsockname()
return port
@classmethod
def setUpClass(cls):
cls.http_server_port = cls.get_available_port()
cls.http_server = HTTPServer(('127.0.0.1', cls.http_server_port), HTTPRequestHandler)
cls.http_server_thread = Thread(target=cls.http_server.serve_forever)
cls.http_server_thread.setDaemon(True)
cls.http_server_thread.start()
@classmethod
def tearDownClass(cls):
cls.http_server.shutdown()
cls.http_server.server_close()
cls.http_server_thread.join()
def setUp(self):
self._conn = MockConnection()
self._addr = ('127.0.0.1', 54382)
self.proxy = Proxy(Client(self._conn, self._addr))
def test_http_get(self):
# Send request line
self.proxy.client.conn.queue((b'GET http://localhost:%d HTTP/1.1' % self.http_server_port) + CRLF)
self.proxy._process_request(self.proxy.client.recv())
self.assertNotEqual(self.proxy.request.state, HttpParser.states.COMPLETE)
# Send headers and blank line, thus completing HTTP request
self.proxy.client.conn.queue(CRLF.join([
b'User-Agent: proxy.py/%s' % version,
b'Host: localhost:%d' % self.http_server_port,
b'Accept: */*',
b'Proxy-Connection: Keep-Alive',
CRLF
]))
self.proxy._process_request(self.proxy.client.recv())
self.assertEqual(self.proxy.request.state, HttpParser.states.COMPLETE)
self.assertEqual(self.proxy.server.addr, (b'localhost', self.http_server_port))
# Flush data queued for server
self.proxy.server.flush()
self.assertEqual(self.proxy.server.buffer_size(), 0)
# Receive full response from server
data = self.proxy.server.recv()
while data:
self.proxy._process_response(data)
logging.info(self.proxy.response.state)
if self.proxy.response.state == HttpParser.states.COMPLETE:
break
data = self.proxy.server.recv()
# Verify 200 success response code
self.assertEqual(self.proxy.response.state, HttpParser.states.COMPLETE)
self.assertEqual(int(self.proxy.response.code), 200)
def test_http_tunnel(self):
self.proxy.client.conn.queue(CRLF.join([
b'CONNECT localhost:%d HTTP/1.1' % self.http_server_port,
b'Host: localhost:%d' % self.http_server_port,
b'User-Agent: proxy.py/%s' % version,
b'Proxy-Connection: Keep-Alive',
CRLF
]))
self.proxy._process_request(self.proxy.client.recv())
self.assertFalse(self.proxy.server is None)
self.assertEqual(self.proxy.client.buffer, PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT)
parser = HttpParser(HttpParser.types.RESPONSE_PARSER)
parser.parse(self.proxy.client.buffer)
self.assertEqual(parser.state, HttpParser.states.HEADERS_COMPLETE)
self.assertEqual(int(parser.code), 200)
self.proxy.client.flush()
self.assertEqual(self.proxy.client.buffer_size(), 0)
self.proxy.client.conn.queue(CRLF.join([
b'GET / HTTP/1.1',
b'Host: localhost:%d' % self.http_server_port,
b'User-Agent: proxy.py/%s' % version,
CRLF
]))
self.proxy._process_request(self.proxy.client.recv())
self.proxy.server.flush()
self.assertEqual(self.proxy.server.buffer_size(), 0)
parser = HttpParser(HttpParser.types.RESPONSE_PARSER)
data = self.proxy.server.recv()
while data:
parser.parse(data)
if parser.state == HttpParser.states.COMPLETE:
break
data = self.proxy.server.recv()
#.........这里部分代码省略.........