本文整理汇总了Python中tornado.httpclient._RequestProxy函数的典型用法代码示例。如果您正苦于以下问题:Python _RequestProxy函数的具体用法?Python _RequestProxy怎么用?Python _RequestProxy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_RequestProxy函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: fetch
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)
开发者ID:Fulo,项目名称:tornado,代码行数:7,代码来源:curl_httpclient.py
示例2: eventsource_connect
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
开发者ID:digideskio,项目名称:tornado-eventsource,代码行数:29,代码来源:event_source_client.py
示例3: websocket_connect
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
开发者ID:zhkzyth,项目名称:tornado-reading-notes,代码行数:25,代码来源:websocket.py
示例4: pingable_ws_connect
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
开发者ID:joshbode,项目名称:nbserverproxy,代码行数:25,代码来源:handlers.py
示例5: websocket_connect
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
开发者ID:alexdxy,项目名称:tornado,代码行数:58,代码来源:websocket.py
示例6: WebSocketConnect
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
开发者ID:Fleischer,项目名称:tornado,代码行数:10,代码来源:websocket.py
示例7: fetch
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}))
开发者ID:astwyg,项目名称:ct-fcore,代码行数:55,代码来源:client.py
示例8: fetch
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
开发者ID:RedhawkSDR,项目名称:rest-python,代码行数:51,代码来源:base.py
示例9: websocket_connect
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
开发者ID:akkakks,项目名称:tornado,代码行数:14,代码来源:websocket.py
示例10: fetch
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)))
开发者ID:CNCBASHER,项目名称:tornado,代码行数:15,代码来源:simple_httpclient.py
示例11: raw_fetch
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
开发者ID:nickwong,项目名称:tornado,代码行数:15,代码来源:httpserver_test.py
示例12: websocket_connect
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
开发者ID:thisisx7,项目名称:tornado-zh,代码行数:47,代码来源:websocket.py
示例13: raw_fetch
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
开发者ID:RyanWarm,项目名称:ZUtils,代码行数:18,代码来源:httpserver_test.py
示例14: websocket_connect
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
开发者ID:animeshinvinci,项目名称:wstunnel,代码行数:18,代码来源:client.py
示例15: fetch
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({})
开发者ID:wishflyer,项目名称:min-python-front,代码行数:41,代码来源:client.py
示例16: test_bad_attribute
def test_bad_attribute(self):
proxy = _RequestProxy(HTTPRequest('http://example.com/'),
dict())
with self.assertRaises(AttributeError):
proxy.foo
开发者ID:CNCBASHER,项目名称:tornado,代码行数:5,代码来源:httpclient_test.py
示例17: test_defaults_none
def test_defaults_none(self):
proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)
self.assertIs(proxy.auth_username, None)
开发者ID:CNCBASHER,项目名称:tornado,代码行数:3,代码来源:httpclient_test.py
示例18: test_both_set
def test_both_set(self):
proxy = _RequestProxy(HTTPRequest('http://example.com/',
proxy_host='foo'),
dict(proxy_host='bar'))
self.assertEqual(proxy.proxy_host, 'foo')
开发者ID:CNCBASHER,项目名称:tornado,代码行数:5,代码来源:httpclient_test.py
示例19: test_neither_set
def test_neither_set(self):
proxy = _RequestProxy(HTTPRequest('http://example.com/'),
dict())
self.assertIs(proxy.auth_username, None)
开发者ID:CNCBASHER,项目名称:tornado,代码行数:4,代码来源:httpclient_test.py
示例20: test_default_set
def test_default_set(self):
proxy = _RequestProxy(HTTPRequest('http://example.com/'),
dict(network_interface='foo'))
self.assertEqual(proxy.network_interface, 'foo')
开发者ID:CNCBASHER,项目名称:tornado,代码行数:4,代码来源:httpclient_test.py
注:本文中的tornado.httpclient._RequestProxy函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论