本文整理汇总了Python中tornado.httpclient.HTTPRequest.body方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPRequest.body方法的具体用法?Python HTTPRequest.body怎么用?Python HTTPRequest.body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httpclient.HTTPRequest
的用法示例。
在下文中一共展示了HTTPRequest.body方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getData
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def getData(self,url,method,data,cookie):
try:
client = HTTPClient()
request = HTTPRequest(
url,
method=method,
headers={
'Cookie':cookie
}
)
if data and method=="GET":
data = json.loads(data)
url = url_concat(url,data)
request.url = url
elif data and method=="POST":
data = json.loads(data)
print data
data = urllib.urlencode(data)
request.body = data
# print request.url
response = client.fetch(request)
return response.body
except Exception,e:
# print str(e)
return None
示例2: getData
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def getData(self,url,method,data,cookie):
try:
client = HTTPClient()
request = HTTPRequest(
url,
method=method,
headers={
'Cookie':cookie
}
)
if data and method=="GET":
url = url_concat(url,data)
url = url.replace("+","%20")
request.url = url
elif data and method=="POST":
realData = {}
for i in data:
realData[i[0]] = i[1]
data = urllib.urlencode(realData)
request.body = data
response = client.fetch(request)
return json.loads(response.body)
except Exception,e:
# print str(e)
#traceback.print_exc()
return str(e)
示例3: _create_http_request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def _create_http_request(self, method, host, port, path,
params=None, data=None, **kwargs):
url = 'http://{host}:{port}{uri}'.format(host=host, port=port, uri=path)
if params and isinstance(params, dict):
url += '?' + urlencode(params)
request = HTTPRequest(
method=method,
url=url,
allow_nonstandard_methods=True,
connect_timeout=self._connect_timeout,
request_timeout=self._request_timeout,
**kwargs
)
if data and method in ['POST', 'PUT', 'PATCH']:
try:
request.body = json.dumps(data)
except TypeError as e:
logging.error(str(e))
raise DBApiError(e)
return request
示例4: perform_request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def perform_request(self, request, response, method):
try:
constants = request_consts[method]
url = request[constants.URL]
timeout = request[constants.TIMEOUT]
http_request = HTTPRequest(url=url, method=method)
http_request.request_timeout = float(timeout)/1000
if method == 'POST':
http_request.body = request[constants.BODY]
#adds cookies to request
params_num = len(request)
if constants.COOKIES <= params_num - 1:
cookies = request[constants.COOKIES]
if len(cookies) > 0:
list_of_cookies = list('{0}={1}'.format(cookie, value) for cookie, value in cookies.iteritems())
cookies_str = '; '.join(list_of_cookies)
http_request.headers.add('Cookie', cookies_str)
#adds headers to request
if constants.HEADERS <= params_num - 1:
for name, values_list in request[constants.HEADERS].iteritems():
for value in values_list:
http_request.headers.add(name, value)
self.logger.info("Downloading {0}, headers {1}, method {2}".format(url, http_request.headers, method))
http_response = yield self.http_client.fetch(http_request)
response_headers = self._get_headers_from_response(http_response)
response.write((True, http_response.body, http_response.code, response_headers,))
response.close()
self.logger.info("{0} has been successfuly downloaded".format(url))
except HTTPError as e:
self.logger.info("Error ({0}) occured while downloading {1}".format(e.message, url))
if e.response is not None:
http_response = e.response
response_headers = self._get_headers_from_response(http_response)
response.write((False, http_response.body, http_response.code, response_headers,))
else:
response.write((False, '', e.code, {},))
response.close()
except socket.gaierror as e:
self.logger.info("Error ({0}) occured while downloading {1}".format(e.message, url))
response.write((False, '', e.errno, {},))
response.close()
except Exception as e:
self.logger.error("Unhandled error ({0}) occured in perform_request, report about this problem "
"to httpclient service developers. Method is {1}, stacktrace is: {2}".format(
e.message, method, traceback.format_exc()))
response.write((False, '', 0, {},))
response.close()
示例5: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def fetch(self, path, body=None, **kwargs):
kwargs['url'] = self.get_url(path)
request = HTTPRequest(**kwargs)
if body is not None:
request.body = body
request.allow_nonstandard_methods = True
self.http_client.fetch(request, self.stop, method=None)
return self.wait()
示例6: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def fetch(self, path, **kwargs):
kwargs['url'] = self.get_url(path)
body = kwargs.pop('body', None)
request = HTTPRequest(**kwargs)
request.body = body
request.allow_nonstandard_methods = True
request.request_timeout = TIMEOUT
self.http_client.fetch(request, self.stop, **kwargs)
return self.wait()
示例7: post
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def post(self):
# param from the client
message = {}
message['status'] = 'error'
message['returned_url'] = 'null'
#url = self.get_argument('url', None)
comic_id = self.get_argument('img', None)
self.threash_hold = self.get_argument('threshold', 90)
if not self.threash_hold:
self.threash_hold = float(90)
else:
self.threash_hold = float(self.threash_hold)
if comic_id:
comic_id = int(comic_id[5:])
if len(self.request.files['file']) > 0:
img_file = self.request.files['file'][0]
# check the client params
if not img_file or comic_id <= 0:
self.write(json.dumps(message))
self.finish()
else:
self.comic_picture_id = comic_id
######################################################
# print self.client_cv_img
filename = img_file['filename']
saved_path = self.IMAGE_PATH + filename
self.client_saved_img = saved_path
if not os.path.exists(filename):
with open(saved_path, "wb") as fp:
fp.write(img_file['body'])
# save the client img
self.client_cv_img = scv.Image(r'%s' % str(saved_path))
######################################################
# ok, we save the client image and gen the SimpleCV img
# we pass the client img url to oxford to get the params
# get parameters
get_params = urllib.urlencode({
'analyzesFaceLandmarks': 'true',
'analyzesAge': 'true',
'analyzesGender': 'true',
'analyzesHeadPose': 'true',
})
url = self.API_URL % get_params
post_params = {}
post_params['url'] = self.HOST + saved_path
#print post_params['url']
# request
request = HTTPRequest(url, method='POST')
request.body = json.dumps(post_params)
request.headers['Content-Type'] = 'application/json'
request.headers['Ocp-Apim-Subscription-key'] = self.TOKEN
# fetch
client = AsyncHTTPClient()
client.fetch(request, self.handleResponse)
示例8: _request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [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)
示例9: do_send_sms
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [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
示例10: _make_proxy_request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def _make_proxy_request(self, request_data):
timeout = float(request_data.get("timeout", DEFAULT_TIMEOUT))
validate_cert = bool(request_data.get("validate_cert") or True)
max_redirects = request_data.get("max_http_redirects") or 0
follow_redirects = max_redirects > 0 # 0 means do not follow redirects
url = request_data.get("url")
params = request_data.get("data")
post_type = request_data.get("post_type")
if params and post_type is None:
url = "%s?%s" % (url, urlencode(params))
logger.info("[%s]agent request url: %s", self.id, url)
proxy_request = HTTPRequest(
url, validate_cert=validate_cert,
headers=self._get_proxy_request_headers(request_data),
method=request_data.get("method", "GET"),
allow_nonstandard_methods=True,
connect_timeout=timeout,
request_timeout=timeout,
streaming_callback=self._streaming_callback,
header_callback=self._header_callback,
follow_redirects=follow_redirects,
max_redirects=max_redirects,
prepare_curl_callback=self.prepare_curl_callback,
)
role_name = request_data.get("role")
if role_name:
InterfaceRoleManager.set_curl_interface_role(
proxy_request, role_name,
)
keystone_auth_info = request_data.get("keystone")
if keystone_auth_info:
logger.warning(
"[%s]agent request required keystone token",
)
auth_headers = yield self._get_keystone_auth_headers(
keystone_auth_info, validate_cert=validate_cert,
)
if not auth_headers:
raise gen.Return()
proxy_request.headers.update(auth_headers)
body = self._get_request_body(request_data)
if body:
proxy_request.body = body
raise gen.Return(proxy_request)
示例11: send_message
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def send_message(url, body=None, method='GET', handler=None):
"""
if handler == None: send a sync request
else handler == <function>: send async request
"""
request = HTTPRequest(url, method=method)
if body:
request.body = body
if handler:
httpclient.AsyncHTTPClient(request, handler)
else:
client = httpclient.HTTPClient()
response = client.fetch(request)
return response.body
示例12: send
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [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))
示例13: post
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def post(self):
ret = {'code':200,'content':''}
try:
url = self.get_argument("url",None)
method = self.get_argument("method",None)
data = self.get_argument("data",None)
if not (url and method):
ret['code'] = 400
ret['content'] = u'参数缺少'
else:
client = HTTPClient()
request = HTTPRequest(
url = url,
method = method
)
if(method=="POST"):
request.body = urllib.urlencode(json.loads(data))
response = client.fetch(request)
ret['content'] = json.loads(response.body)
except Exception,e:
ret['code'] = 500
ret['content'] = u'系统错误'
示例14: test_body_setter
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [as 别名]
def test_body_setter(self):
request = HTTPRequest('http://example.com')
request.body = 'foo'
self.assertEqual(request.body, utf8('foo'))
示例15: request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import body [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