當前位置: 首頁>>代碼示例>>Python>>正文


Python Proxy._process_request方法代碼示例

本文整理匯總了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()
#.........這裏部分代碼省略.........
開發者ID:abhinavsingh,項目名稱:proxy.py,代碼行數:103,代碼來源:tests.py


注:本文中的proxy.Proxy._process_request方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。