本文整理汇总了Python中tornado.httpclient.HTTPRequest.method方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPRequest.method方法的具体用法?Python HTTPRequest.method怎么用?Python HTTPRequest.method使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httpclient.HTTPRequest
的用法示例。
在下文中一共展示了HTTPRequest.method方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import method [as 别名]
def _request(self, extension, body, callback):
"""Wraps tornado syntax for sending an HTTP request."""
request = HTTPRequest('http://{}/{}'.format(self.ip, extension),
connect_timeout=self.timeout,
request_timeout=self.timeout)
if body:
request.body = body
request.method = 'POST'
if extension == 'ToolWeb/Cmd':
request.headers = {'Content-Type': 'text/xml'}
self.client.fetch(request, callback)
示例2: do_send_sms
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import method [as 别名]
def do_send_sms(cell, data):
http_client = HTTPClient()
sms_url = "http://172.31.11.203:8080/notify/sms/"
request = HTTPRequest(sms_url)
request.headers["Content-Type"] = "application/json"
request.headers["HTTP_HEAD_ENCODING"] = "utf-8"
request.method = "POST"
request.body = '{"id":"0","phones":"' + cell + '","content":"' + data + '"}'
resp = http_client.fetch(request)
if resp.code != 201:
raise RuntimeError("SMS Gateway Error: " + str(resp.code))
print resp.code, resp.body
示例3: send
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import method [as 别名]
def send(self, message):
"""
Send a message to API URL
"""
send_url = '{}/{}'.format(self.url, self.get_api_path_equivalent(message=message))
logger.debug("EventReporter API URL = {}".format(send_url))
request = HTTPRequest(url=send_url)
request.method = 'POST'
request.headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
request.body = message.to_json()
response = None
try:
#noinspection PyTypeChecker
response = self.http_client.fetch(request=request)
logger.debug("Response from Event Reporter: {}".format(response.body))
except httpclient.HTTPError, e:
logger.error("Error when trying to contact Event Reporter! Error: {}".format(e))
示例4: request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import method [as 别名]
def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True, _request_timeout=None):
"""
:param method: http request method
:param url: http request url
:param query_params: query parameters in the url
:param headers: http request headers
:param body: request json body, for `application/json`
:param post_params: request post parameters,
`application/x-www-form-urlencoded`
and `multipart/form-data`
:param _preload_content: this is a non-applicable field for the AiohttpClient.
:param _request_timeout: timeout setting for this request. If one number provided, it will be total request
timeout. It can also be a pair (tuple) of (connection, read) timeouts.
"""
method = method.upper()
assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH', 'OPTIONS']
if post_params and body:
raise ValueError(
"body parameter cannot be used with post_params parameter."
)
request = HTTPRequest(url)
request.ssl_context = self.ssl_context
request.proxy_host = self.proxy_host
request.proxy_port = self.proxy_port
request.method = method
if headers:
request.headers = headers
if 'Content-Type' not in headers:
request.headers['Content-Type'] = 'application/json'
request.request_timeout = _request_timeout or 5 * 60
post_params = post_params or {}
if query_params:
request.url += '?' + urlencode(query_params)
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
if re.search('json', headers['Content-Type'], re.IGNORECASE):
if body:
body = json.dumps(body)
request.body = body
elif headers['Content-Type'] == 'application/x-www-form-urlencoded':
request.body = urlencode(post_params)
# TODO: transform to multipart form
elif headers['Content-Type'] == 'multipart/form-data':
request.body = encode_multipart_formdata(post_params)
# Pass a `bytes` parameter directly in the body to support
# other content types than Json when `body` argument is provided
# in serialized form
elif isinstance(body, bytes):
request.body = body
else:
# Cannot generate the request from given parameters
msg = """Cannot prepare a request message for provided arguments.
Please check that your arguments match declared content type."""
raise ApiException(status=0, reason=msg)
r = yield self.pool_manager.fetch(request)
r = RESTResponse(r, r.body)
# log response body
logger.debug("response body: %s", r.data)
if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)
return r