def eventsource_connect(url, io_loop=None, callback=None, connect_timeout=None):
    """Client-side eventsource support.

    Takes a url and returns a Future whose result is a
    `EventSourceClient`.

    """
    if io_loop is None:
        io_loop = IOLoop.current()
    if isinstance(url, httpclient.HTTPRequest):
        assert connect_timeout is None
        request = url
        # Copy and convert the headers dict/object (see comments in
        # AsyncHTTPClient.fetch)
        request.headers = httputil.HTTPHeaders(request.headers)
    else:
        request = httpclient.HTTPRequest(
            url,
            connect_timeout=connect_timeout,
            headers=httputil.HTTPHeaders({
                "Accept-Encoding": "identity"
            })
        )
    request = httpclient._RequestProxy(
        request, httpclient.HTTPRequest._DEFAULTS)
    conn = EventSourceClient(io_loop, request)
    if callback is not None:
        io_loop.add_future(conn.connect_future, callback)
    return conn.connect_future
Ejemplo n.º 2
0
 def fetch(self, request, callback, **kwargs):
     if not isinstance(request, HTTPRequest):
         request = HTTPRequest(url=request, **kwargs)
     request = _RequestProxy(request, self.defaults)
     self._requests.append((request, stack_context.wrap(callback)))
     self._process_queue()
     self._set_timeout(0)
Ejemplo n.º 3
0
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`.

    .. versionchanged:: 3.2
       Also accepts ``HTTPRequest`` objects in place of urls.
    """
    if io_loop is None:
        io_loop = IOLoop.current()
    if isinstance(url, httpclient.HTTPRequest):
        assert connect_timeout is None
        request = url
        # Copy and convert the headers dict/object (see comments in
        # AsyncHTTPClient.fetch)
        request.headers = httputil.HTTPHeaders(request.headers)
    else:
        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
Ejemplo n.º 4
0
def pingable_ws_connect(request=None, on_message_callback=None,
                        on_ping_callback=None):
    """
    A variation on websocket_connect that returns a PingableWSClientConnection
    with on_ping_callback.
    """
    # Copy and convert the headers dict/object (see comments in
    # AsyncHTTPClient.fetch)
    request.headers = httputil.HTTPHeaders(request.headers)
    request = httpclient._RequestProxy(
        request, httpclient.HTTPRequest._DEFAULTS)

    # for tornado 4.5.x compatibility
    if version_info[0] == 4:
        conn = PingableWSClientConnection(io_loop=ioloop.IOLoop.current(),
            request=request,
            on_message_callback=on_message_callback,
            on_ping_callback=on_ping_callback)
    else:
        conn = PingableWSClientConnection(request=request,
            on_message_callback=on_message_callback,
            on_ping_callback=on_ping_callback,
            max_message_size=getattr(websocket, '_default_max_message_size', 10 * 1024 * 1024))

    return conn.connect_future
Ejemplo n.º 5
0
def websocket_connect(url, callback=None, connect_timeout=None,
                      on_message_callback=None, compression_options=None,
                      ping_interval=None, ping_timeout=None,
                      max_message_size=None):
    """Client-side websocket support.

    Takes a url and returns a Future whose result is a
    `WebSocketClientConnection`.

    ``compression_options`` is interpreted in the same way as the
    return value of `.WebSocketHandler.get_compression_options`.

    The connection supports two styles of operation. In the coroutine
    style, the application typically calls
    `~.WebSocketClientConnection.read_message` in a loop::

        conn = yield websocket_connect(url)
        while True:
            msg = yield conn.read_message()
            if msg is None: break
            # Do something with msg

    In the callback style, pass an ``on_message_callback`` to
    ``websocket_connect``. In both styles, a message of ``None``
    indicates that the connection has been closed.

    .. versionchanged:: 3.2
       Also accepts ``HTTPRequest`` objects in place of urls.

    .. versionchanged:: 4.1
       Added ``compression_options`` and ``on_message_callback``.

    .. versionchanged:: 4.5
       Added the ``ping_interval``, ``ping_timeout``, and ``max_message_size``
       arguments, which have the same meaning as in `WebSocketHandler`.

    .. versionchanged:: 5.0
       The ``io_loop`` argument (deprecated since version 4.1) has been removed.
    """
    if isinstance(url, httpclient.HTTPRequest):
        assert connect_timeout is None
        request = url
        # Copy and convert the headers dict/object (see comments in
        # AsyncHTTPClient.fetch)
        request.headers = httputil.HTTPHeaders(request.headers)
    else:
        request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout)
    request = httpclient._RequestProxy(
        request, httpclient.HTTPRequest._DEFAULTS)
    conn = WebSocketClientConnection(request,
                                     on_message_callback=on_message_callback,
                                     compression_options=compression_options,
                                     ping_interval=ping_interval,
                                     ping_timeout=ping_timeout,
                                     max_message_size=max_message_size)
    if callback is not None:
        IOLoop.current().add_future(conn.connect_future, callback)
    return conn.connect_future
Ejemplo n.º 6
0
def WebSocketConnect(url, io_loop=None, callback=None):
    if io_loop is None:
        io_loop = IOLoop.current()
    request = httpclient.HTTPRequest(url)
    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
Ejemplo n.º 7
0
    def fetch(self, url, headers=None, body=None, method="GET", callback=None, raise_error=True, cache=None, **kwargs):
        headers = headers or {}
        body = body or "{}"
        """very simlar with AsyncHTTPClient.fetch
        """
        if self._closed:
            raise RuntimeError("fetch() called on closed AsyncHTTPClient")
        future = TracebackFuture()
        if isinstance(body, dict):
            for k,v in body.items():
                if v is None:
                    del body[k]
            body = urllib.urlencode(body)
        for k,v in headers.items(): #headers 只能接收str
            if v:
                headers[k] = str(headers[k])
            else:
                del headers[k]
        request = HTTPRequest(url=url,method=method,headers=headers,body=body, allow_nonstandard_methods=True, request_timeout=600 ,**kwargs)
        # We may modify this (to add Host, Accept-Encoding, etc),
        # so make sure we don't modify the caller's object.  This is also
        # where normal dicts get converted to HTTPHeaders objects.
        request.headers = httputil.HTTPHeaders(request.headers)
        request = _RequestProxy(request, self.defaults)
        if callback is not None:
            callback = stack_context.wrap(callback)

            def handle_future(future):
                exc = future.exception()
                if isinstance(exc, HTTPError) and exc.response is not None:
                    response = exc.response
                elif exc is not None:
                    response = HTTPResponse(
                        request, 599, error=exc,
                        request_time=time.time() - request.start_time)
                else:
                    response = future.result()
                self.io_loop.add_callback(callback, response)
            future.add_done_callback(handle_future)

        def handle_response(response):
            if raise_error and response.error:
                future.set_exception(response.error)
            else:
                try:
                    resp = json.loads(str(response.body))
                    if resp.get("statusCode") and resp.get("statusCode")==800:
                        future.set_result(resp)
                        log.info(json.dumps({"response":resp,"body":body,"headers":headers,"url":url}))
                    else:
                        future.set_result({"error_type":"statusCode is not 800", "response":resp,"body":body,"headers":headers,"url":url})
                        log.error(json.dumps({"error_type":"statusCode is not 800", "response":resp,"body":body,"headers":headers,"url":url}))
                except Exception,e:
                    future.set_result({"error_type":"json.loads failed!","error":str(e),"response.body":response.body,"body":body,"headers":headers,"url":url})
                    log.error(json.dumps({"error_type":"json.loads failed!","error":str(e),"response.body":response.body,"body":body,"headers":headers,"url":url}))
Ejemplo n.º 8
0
  def fetch(self, request, callback=None, raise_error=True, **kwargs):
        """Executes a request, asynchronously returning an `HTTPResponse`.

        The request may be either a string URL or an `HTTPRequest` object.
        If it is a string, we construct an `HTTPRequest` using any additional
        kwargs: ``HTTPRequest(request, **kwargs)``

        This method returns a `.Future` whose result is an
        `HTTPResponse`.  By default, the ``Future`` will raise an `HTTPError`
        if the request returned a non-200 response code. Instead, if
        ``raise_error`` is set to False, the response will always be
        returned regardless of the response code.

        If a ``callback`` is given, it will be invoked with the `HTTPResponse`.
        In the callback interface, `HTTPError` is not automatically raised.
        Instead, you must check the response's ``error`` attribute or
        call its `~HTTPResponse.rethrow` method.
        """
        if self._closed:
            raise RuntimeError("fetch() called on closed AsyncHTTPClient")
        if not isinstance(request, HTTPRequest):
            request = HTTPRequest(url=request, **kwargs)
        # We may modify this (to add Host, Accept-Encoding, etc),
        # so make sure we don't modify the caller's object.  This is also
        # where normal dicts get converted to HTTPHeaders objects.
        request.headers = httputil.HTTPHeaders(request.headers)
        request = _RequestProxy(request, self.defaults)
        future = TracebackFuture()
        if callback is not None:
            callback = stack_context.wrap(callback)

            def handle_future(future):
                exc = future.exception()
                if isinstance(exc, HTTPError) and exc.response is not None:
                    response = exc.response
                elif exc is not None:
                    response = HTTPResponse(
                        request, 599, error=exc,
                        request_time=time.time() - request.start_time)
                else:
                    response = future.result()
                self.io_loop.add_callback(callback, response)
            future.add_done_callback(handle_future)

        def handle_response(response):
            if raise_error and response.error:
                future.set_exception(response.error)
            else:
                future.set_result(response)
        self.fetch_impl(request, handle_response)
        return future
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
 def raw_fetch(self, headers, body):
     client = SimpleAsyncHTTPClient(self.io_loop)
     conn = RawRequestHTTPConnection(
         self.io_loop,
         client,
         httpclient._RequestProxy(httpclient.HTTPRequest(self.get_url("/")), dict(httpclient.HTTPRequest._DEFAULTS)),
         None,
         self.stop,
         1024 * 1024,
     )
     conn.set_request(b("\r\n").join(headers + [utf8("Content-Length: %d\r\n" % len(body))]) + b("\r\n") + body)
     response = self.wait()
     client.close()
     response.rethrow()
     return response
Ejemplo n.º 11
0
 def fetch(self, request, callback, **kwargs):
     if not isinstance(request, HTTPRequest):
         request = HTTPRequest(url=request, **kwargs)
     # We're going to modify this (to add Host, Accept-Encoding, etc),
     # so make sure we don't modify the caller's object.  This is also
     # where normal dicts get converted to HTTPHeaders objects.
     request.headers = HTTPHeaders(request.headers)
     request = _RequestProxy(request, self.defaults)
     callback = stack_context.wrap(callback)
     self.queue.append((request, callback))
     self._process_queue()
     if self.queue:
         gen_log.debug("max_clients limit reached, request queued. "
                       "%d active, %d queued requests." % (
                 len(self.active), len(self.queue)))
Ejemplo n.º 12
0
def websocket_connect(url, io_loop=None, callback=None, connect_timeout=None,
                      on_message_callback=None, compression_options=None):

    """客户端 WebSocket 支持
    需要指定 url, 返回一个结果为 `WebSocketClientConnection` 的 Future 对象

    ``compression_options`` 作为 `.WebSocketHandler.get_compression_options` 的
    返回值, 将会以同样的方式执行.

    这个连接支持两种类型的操作.在协程风格下,应用程序通常在一个循环里调用`~.WebSocket
    ClientConnection.read_message`::

        conn = yield websocket_connect(url)
        while True:
            msg = yield conn.read_message()
            if msg is None: break
            # Do something with msg


    在回调风格下,需要传递 ``on_message_callback`` 到 ``websocket_connect`` 里.
    在这两种风格里,一个内容是 ``None`` 的 message 都标志着 WebSocket 连接已经.

    .. versionchanged:: 3.2
       允许使用 ``HTTPRequest`` 对象来代替 urls.
    .. versionchanged:: 4.1
       添加 ``compression_options`` 和 ``on_message_callback`` .

       不赞成使用 ``compression_options`` .
    """
    if io_loop is None:
        io_loop = IOLoop.current()
    if isinstance(url, httpclient.HTTPRequest):
        assert connect_timeout is None
        request = url
        # Copy and convert the headers dict/object (see comments in
        # AsyncHTTPClient.fetch)
        request.headers = httputil.HTTPHeaders(request.headers)
    else:
        request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout)
    request = httpclient._RequestProxy(
        request, httpclient.HTTPRequest._DEFAULTS)
    conn = WebSocketClientConnection(io_loop, request,
                                     on_message_callback=on_message_callback,
                                     compression_options=compression_options)
    if callback is not None:
        io_loop.add_future(conn.connect_future, callback)
    return conn.connect_future
Ejemplo n.º 13
0
 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
Ejemplo n.º 14
0
def websocket_connect(url, io_loop=None, callback=None, connect_timeout=None, **kwargs):
    """Client-side websocket support.

    Takes a url and returns a Future whose result is a
    `WebSocketClientConnection`.
    """
    options = httpclient.HTTPRequest._DEFAULTS.copy()
    options.update(kwargs)

    if io_loop is None:
        io_loop = IOLoop.current()
    request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout,
                                     validate_cert=kwargs.get("validate_cert", True))
    request = httpclient._RequestProxy(request, options)
    conn = WebSocketClientConnection(io_loop, request)
    if callback is not None:
        io_loop.add_future(conn.connect_future, callback)
    return conn.connect_future
Ejemplo n.º 15
0
    def fetch(self, url, headers=None, body=None, method="GET", callback=None, raise_error=True, **kwargs):
        """very simlar with AsyncHTTPClient.fetch
        """
        if self._closed:
            raise RuntimeError("fetch() called on closed AsyncHTTPClient")
        if isinstance(body, dict):
            body = self.dict2form(body)
            print (body)
        request = HTTPRequest(url=url,method=method,headers=headers,body=body, allow_nonstandard_methods=True, **kwargs)
        # We may modify this (to add Host, Accept-Encoding, etc),
        # so make sure we don't modify the caller's object.  This is also
        # where normal dicts get converted to HTTPHeaders objects.
        request.headers = httputil.HTTPHeaders(request.headers)
        request = _RequestProxy(request, self.defaults)
        future = TracebackFuture()
        if callback is not None:
            callback = stack_context.wrap(callback)

            def handle_future(future):
                exc = future.exception()
                if isinstance(exc, HTTPError) and exc.response is not None:
                    response = exc.response
                elif exc is not None:
                    response = HTTPResponse(
                        request, 599, error=exc,
                        request_time=time.time() - request.start_time)
                else:
                    response = future.result()
                self.io_loop.add_callback(callback, response)
            future.add_done_callback(handle_future)

        def handle_response(response):
            if raise_error and response.error:
                future.set_exception(response.error)
            else:
                try:
                    resp = json.loads(str(response.body))
                    future.set_result(resp)
                except Exception,e:
                    print (e)
                    future.set_result({})
Ejemplo n.º 16
0
def websocket_connect(url,
                      callback=None,
                      connect_timeout=None,
                      on_message_callback=None,
                      compression_options=None,
                      ping_interval=None,
                      ping_timeout=None,
                      max_message_size=None):
    """Client-side websocket support.

    Takes a url and returns a Future whose result is a
    `WebSocketClientConnection`.

    ``compression_options`` is interpreted in the same way as the
    return value of `.WebSocketHandler.get_compression_options`.

    The connection supports two styles of operation. In the coroutine
    style, the application typically calls
    `~.WebSocketClientConnection.read_message` in a loop::

        conn = yield websocket_connect(url)
        while True:
            msg = yield conn.read_message()
            if msg is None: break
            # Do something with msg

    In the callback style, pass an ``on_message_callback`` to
    ``websocket_connect``. In both styles, a message of ``None``
    indicates that the connection has been closed.

    .. versionchanged:: 3.2
       Also accepts ``HTTPRequest`` objects in place of urls.

    .. versionchanged:: 4.1
       Added ``compression_options`` and ``on_message_callback``.

    .. versionchanged:: 4.5
       Added the ``ping_interval``, ``ping_timeout``, and ``max_message_size``
       arguments, which have the same meaning as in `WebSocketHandler`.

    .. versionchanged:: 5.0
       The ``io_loop`` argument (deprecated since version 4.1) has been removed.
    """
    if isinstance(url, httpclient.HTTPRequest):
        assert connect_timeout is None
        request = url
        # Copy and convert the headers dict/object (see comments in
        # AsyncHTTPClient.fetch)
        request.headers = httputil.HTTPHeaders(request.headers)
    else:
        request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout)
    request = httpclient._RequestProxy(request,
                                       httpclient.HTTPRequest._DEFAULTS)
    conn = WebSocketClientConnection(request,
                                     on_message_callback=on_message_callback,
                                     compression_options=compression_options,
                                     ping_interval=ping_interval,
                                     ping_timeout=ping_timeout,
                                     max_message_size=max_message_size)
    if callback is not None:
        IOLoop.current().add_future(conn.connect_future, callback)
    return conn.connect_future
Ejemplo n.º 17
0
 def test_neither_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'), dict())
     self.assertIs(proxy.auth_username, None)
Ejemplo n.º 18
0
 def test_request_set(self):
     proxy = _RequestProxy(
         HTTPRequest('http://example.com/', user_agent='foo'), dict())
     self.assertEqual(proxy.user_agent, 'foo')
Ejemplo n.º 19
0
 def test_default_set(self):
     proxy = _RequestProxy(HTTPRequest("http://example.com/"),
                           dict(network_interface="foo"))
     self.assertEqual(proxy.network_interface, "foo")
Ejemplo n.º 20
0
 def test_both_set(self):
     proxy = _RequestProxy(
         HTTPRequest("http://example.com/", proxy_host="foo"),
         dict(proxy_host="bar"))
     self.assertEqual(proxy.proxy_host, "foo")
Ejemplo n.º 21
0
 def test_both_set(self):
     proxy = _RequestProxy(
         HTTPRequest('http://example.com/', proxy_host='foo'),
         dict(proxy_host='bar'))
     self.assertEqual(proxy.proxy_host, 'foo')
Ejemplo n.º 22
0
 def test_both_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/',
                                       proxy_host='foo'),
                           dict(proxy_host='bar'))
     self.assertEqual(proxy.proxy_host, 'foo')
Ejemplo n.º 23
0
 def test_request_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/',
                                       user_agent='foo'),
                           dict())
     self.assertEqual(proxy.user_agent, 'foo')
Ejemplo n.º 24
0
 def test_bad_attribute(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                           dict())
     with self.assertRaises(AttributeError):
         proxy.foo
Ejemplo n.º 25
0
 def test_bad_attribute(self):
     proxy = _RequestProxy(HTTPRequest("http://example.com/"), dict())
     with self.assertRaises(AttributeError):
         proxy.foo
Ejemplo n.º 26
0
 def test_defaults_none(self):
     proxy = _RequestProxy(HTTPRequest("http://example.com/"), None)
     self.assertIs(proxy.auth_username, None)
Ejemplo n.º 27
0
 def test_request_set(self):
     proxy = _RequestProxy(
         HTTPRequest("http://example.com/", user_agent="foo"), dict())
     self.assertEqual(proxy.user_agent, "foo")
Ejemplo n.º 28
0
 def test_default_set(self):
     proxy = _RequestProxy(
         HTTPRequest("http://example.com/"), dict(network_interface="foo")
     )
     self.assertEqual(proxy.network_interface, "foo")
Ejemplo n.º 29
0
 def test_default_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                           dict(network_interface='foo'))
     self.assertEqual(proxy.network_interface, 'foo')
Ejemplo n.º 30
0
 def test_both_set(self):
     proxy = _RequestProxy(
         HTTPRequest("http://example.com/", proxy_host="foo"), dict(proxy_host="bar")
     )
     self.assertEqual(proxy.proxy_host, "foo")
Ejemplo n.º 31
0
 def test_neither_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                           dict())
     self.assertIs(proxy.auth_username, None)
Ejemplo n.º 32
0
 def test_default_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                           dict(network_interface='foo'))
     self.assertEqual(proxy.network_interface, 'foo')
Ejemplo n.º 33
0
 def test_defaults_none(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)
     self.assertIs(proxy.auth_username, None)
Ejemplo n.º 34
0
 def test_request_set(self):
     proxy = _RequestProxy(
         HTTPRequest("http://example.com/", user_agent="foo"), dict()
     )
     self.assertEqual(proxy.user_agent, "foo")