Python tornado.httpclient.HTTPRequest() Examples

The following are 30 code examples of tornado.httpclient.HTTPRequest(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.httpclient , or try the search function .
Example #1
Source File: tornado.py    From python-consul2 with MIT License 6 votes vote down vote up
def get(self, callback, path, params=None, headers=None):
        uri = self.uri(path, params)
        request = httpclient.HTTPRequest(uri,
                                         method='GET',
                                         validate_cert=self.verify,
                                         headers=headers)
        return self._request(callback, request) 
Example #2
Source File: tornado.py    From python-consul2 with MIT License 6 votes vote down vote up
def post(self, callback, path, params=None, data='', headers=None):
        uri = self.uri(path, params)
        request = httpclient.HTTPRequest(uri,
                                         method='POST',
                                         body=data,
                                         validate_cert=self.verify,
                                         headers=headers)
        return self._request(callback, request) 
Example #3
Source File: httpserver_test.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def raw_fetch(self, headers, body):
        with closing(Resolver(io_loop=self.io_loop)) as resolver:
            with closing(SimpleAsyncHTTPClient(self.io_loop,
                                               resolver=resolver)) as client:
                conn = RawRequestHTTPConnection(
                    self.io_loop, client,
                    httpclient._RequestProxy(
                        httpclient.HTTPRequest(self.get_url("/")),
                        dict(httpclient.HTTPRequest._DEFAULTS)),
                    None, self.stop,
                    1024 * 1024, resolver)
                conn.set_request(
                    b"\r\n".join(headers +
                                 [utf8("Content-Length: %d\r\n" % len(body))]) +
                    b"\r\n" + body)
                response = self.wait()
                response.rethrow()
                return response 
Example #4
Source File: httpserver_test.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def raw_fetch(self, headers, body):
        with closing(Resolver(io_loop=self.io_loop)) as resolver:
            with closing(SimpleAsyncHTTPClient(self.io_loop,
                                               resolver=resolver)) as client:
                conn = RawRequestHTTPConnection(
                    self.io_loop, client,
                    httpclient._RequestProxy(
                        httpclient.HTTPRequest(self.get_url("/")),
                        dict(httpclient.HTTPRequest._DEFAULTS)),
                    None, self.stop,
                    1024 * 1024, resolver)
                conn.set_request(
                    b"\r\n".join(headers +
                                 [utf8("Content-Length: %d\r\n" % len(body))]) +
                    b"\r\n" + body)
                response = self.wait()
                response.rethrow()
                return response 
Example #5
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_bad_attribute(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                              dict())
        with self.assertRaises(AttributeError):
            proxy.foo 
Example #6
Source File: httpclient_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_default_set(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                              dict(network_interface='foo'))
        self.assertEqual(proxy.network_interface, 'foo') 
Example #7
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_defaults_none(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)
        self.assertIs(proxy.auth_username, None) 
Example #8
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_str(self):
        response = HTTPResponse(HTTPRequest('http://example.com'),
                                200, headers={}, buffer=BytesIO())
        s = str(response)
        self.assertTrue(s.startswith('HTTPResponse('))
        self.assertIn('code=200', s) 
Example #9
Source File: curl_httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_prepare_curl_callback_stack_context(self):
        exc_info = []

        def error_handler(typ, value, tb):
            exc_info.append((typ, value, tb))
            self.stop()
            return True

        with ExceptionStackContext(error_handler):
            request = HTTPRequest(self.get_url('/'),
                                  prepare_curl_callback=lambda curl: 1 / 0)
        self.http_client.fetch(request, callback=self.stop)
        self.wait()
        self.assertEqual(1, len(exc_info))
        self.assertIs(exc_info[0][0], ZeroDivisionError) 
Example #10
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_invalid_subdomains(self):
        port = self.get_http_port()

        url = 'ws://localhost:%d/echo' % port
        # Subdomains should be disallowed by default.  If we could pass a
        # resolver to websocket_connect we could test sibling domains as well.
        headers = {'Origin': 'http://subtenant.localhost'}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))

        self.assertEqual(cm.exception.code, 403) 
Example #11
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_websocket_header_echo(self):
        # Ensure that headers can be returned in the response.
        # Specifically, that arbitrary headers passed through websocket_connect
        # can be returned.
        ws = yield websocket_connect(
            HTTPRequest('ws://127.0.0.1:%d/header_echo' % self.get_http_port(),
                        headers={'X-Test-Hello': 'hello'}))
        self.assertEqual(ws.headers.get('X-Test-Hello'), 'hello')
        self.assertEqual(ws.headers.get('X-Extra-Response-Header'), 'Extra-Response-Value')
        yield self.close(ws) 
Example #12
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_valid_no_path(self):
        port = self.get_http_port()

        url = 'ws://127.0.0.1:%d/echo' % port
        headers = {'Origin': 'http://127.0.0.1:%d' % port}

        ws = yield websocket_connect(HTTPRequest(url, headers=headers))
        ws.write_message('hello')
        response = yield ws.read_message()
        self.assertEqual(response, 'hello')
        yield self.close(ws) 
Example #13
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_valid_with_path(self):
        port = self.get_http_port()

        url = 'ws://127.0.0.1:%d/echo' % port
        headers = {'Origin': 'http://127.0.0.1:%d/something' % port}

        ws = yield websocket_connect(HTTPRequest(url, headers=headers))
        ws.write_message('hello')
        response = yield ws.read_message()
        self.assertEqual(response, 'hello')
        yield self.close(ws) 
Example #14
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_invalid_partial_url(self):
        port = self.get_http_port()

        url = 'ws://127.0.0.1:%d/echo' % port
        headers = {'Origin': '127.0.0.1:%d' % port}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))
        self.assertEqual(cm.exception.code, 403) 
Example #15
Source File: httpclient_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_request_set(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/',
                                          user_agent='foo'),
                              dict())
        self.assertEqual(proxy.user_agent, 'foo') 
Example #16
Source File: views.py    From MPContribs with MIT License 5 votes vote down vote up
def connect_kernel():
    # TODO check status busy/idle
    run_sync(manager.list_kernels())
    kernels = {
        kernel_id: dateparser.parse(kernel["last_activity"])
        for kernel_id, kernel in manager._kernels.items()
    }
    kernel_id = url_escape(sorted(kernels, key=kernels.get)[0])
    client = GatewayClient.instance()
    url = url_path_join(client.ws_url, client.kernels_endpoint, kernel_id, "channels")
    ws_req = HTTPRequest(url=url)
    return run_sync(websocket_connect(ws_req)) 
Example #17
Source File: curl_httpclient_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_prepare_curl_callback_stack_context(self):
        exc_info = []

        def error_handler(typ, value, tb):
            exc_info.append((typ, value, tb))
            self.stop()
            return True

        with ExceptionStackContext(error_handler):
            request = HTTPRequest(self.get_url('/'),
                                  prepare_curl_callback=lambda curl: 1 / 0)
        self.http_client.fetch(request, callback=self.stop)
        self.wait()
        self.assertEqual(1, len(exc_info))
        self.assertIs(exc_info[0][0], ZeroDivisionError) 
Example #18
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_both_set(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/',
                                          proxy_host='foo'),
                              dict(proxy_host='bar'))
        self.assertEqual(proxy.proxy_host, 'foo') 
Example #19
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_default_set(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                              dict(network_interface='foo'))
        self.assertEqual(proxy.network_interface, 'foo') 
Example #20
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_request_set(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/',
                                          user_agent='foo'),
                              dict())
        self.assertEqual(proxy.user_agent, 'foo') 
Example #21
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_reuse_request_from_response(self):
        # The response.request attribute should be an HTTPRequest, not
        # a _RequestProxy.
        # This test uses self.http_client.fetch because self.fetch calls
        # self.get_url on the input unconditionally.
        url = self.get_url('/hello')
        response = yield self.http_client.fetch(url)
        self.assertEqual(response.request.url, url)
        self.assertTrue(isinstance(response.request, HTTPRequest))
        response2 = yield self.http_client.fetch(response.request)
        self.assertEqual(response2.body, b'Hello world!') 
Example #22
Source File: websocket.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def websocket_connect(url, io_loop=None, callback=None, connect_timeout=None):
    """Client-side websocket support.

    Takes a url and returns a Future whose result is a
    `WebSocketClientConnection`.
    """
    if io_loop is None:
        io_loop = IOLoop.current()
    request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout)
    request = httpclient._RequestProxy(
        request, httpclient.HTTPRequest._DEFAULTS)
    conn = WebSocketClientConnection(io_loop, request)
    if callback is not None:
        io_loop.add_future(conn.connect_future, callback)
    return conn.connect_future 
Example #23
Source File: base.py    From tornado-botocore with MIT License 5 votes vote down vote up
def _send_request(self, request_dict, operation_model, callback=None):
        request = self.endpoint.create_request(request_dict, operation_model)

        req_body = getattr(request.body, 'buf', request.body)

        request = HTTPRequest(
            url=request.url,
            headers=request.headers,
            method=request.method,
            body=req_body,
            validate_cert=False,
            proxy_host=self.proxy_host,
            proxy_port=self.proxy_port,
            connect_timeout=self.connect_timeout,
            request_timeout=self.request_timeout
        )

        if callback is None:
            # sync
            return self._process_response(
                HTTPClient().fetch(request),
                operation_model=operation_model
            )

        # async
        self.http_client.fetch(
            request,
            callback=partial(
                self._process_response,
                callback=callback,
                operation_model=operation_model
            )
        ) 
Example #24
Source File: websocket_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_check_origin_invalid_subdomains(self):
        port = self.get_http_port()

        url = "ws://localhost:%d/echo" % port
        # Subdomains should be disallowed by default.  If we could pass a
        # resolver to websocket_connect we could test sibling domains as well.
        headers = {"Origin": "http://subtenant.localhost"}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))

        self.assertEqual(cm.exception.code, 403) 
Example #25
Source File: websocket_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_check_origin_invalid(self):
        port = self.get_http_port()

        url = "ws://127.0.0.1:%d/echo" % port
        # Host is 127.0.0.1, which should not be accessible from some other
        # domain
        headers = {"Origin": "http://somewhereelse.com"}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))

        self.assertEqual(cm.exception.code, 403) 
Example #26
Source File: websocket_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_check_origin_valid_with_path(self):
        port = self.get_http_port()

        url = "ws://127.0.0.1:%d/echo" % port
        headers = {"Origin": "http://127.0.0.1:%d/something" % port}

        ws = yield websocket_connect(HTTPRequest(url, headers=headers))
        ws.write_message("hello")
        response = yield ws.read_message()
        self.assertEqual(response, "hello") 
Example #27
Source File: websocket_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_check_origin_valid_no_path(self):
        port = self.get_http_port()

        url = "ws://127.0.0.1:%d/echo" % port
        headers = {"Origin": "http://127.0.0.1:%d" % port}

        ws = yield websocket_connect(HTTPRequest(url, headers=headers))
        ws.write_message("hello")
        response = yield ws.read_message()
        self.assertEqual(response, "hello") 
Example #28
Source File: websocket_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_websocket_header_echo(self):
        # Ensure that headers can be returned in the response.
        # Specifically, that arbitrary headers passed through websocket_connect
        # can be returned.
        ws = yield websocket_connect(
            HTTPRequest(
                "ws://127.0.0.1:%d/header_echo" % self.get_http_port(),
                headers={"X-Test-Hello": "hello"},
            )
        )
        self.assertEqual(ws.headers.get("X-Test-Hello"), "hello")
        self.assertEqual(
            ws.headers.get("X-Extra-Response-Header"), "Extra-Response-Value"
        ) 
Example #29
Source File: websocket_test.py    From opendevops with GNU General Public License v3.0 5 votes vote down vote up
def test_websocket_headers(self):
        # Ensure that arbitrary headers can be passed through websocket_connect.
        ws = yield websocket_connect(
            HTTPRequest(
                "ws://127.0.0.1:%d/header" % self.get_http_port(),
                headers={"X-Test": "hello"},
            )
        )
        response = yield ws.read_message()
        self.assertEqual(response, "hello") 
Example #30
Source File: httpclient_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_both_set(self):
        proxy = _RequestProxy(HTTPRequest('http://example.com/',
                                          proxy_host='foo'),
                              dict(proxy_host='bar'))
        self.assertEqual(proxy.proxy_host, 'foo')