本文整理汇总了Python中six.moves.urllib.request.Request.data方法的典型用法代码示例。如果您正苦于以下问题:Python Request.data方法的具体用法?Python Request.data怎么用?Python Request.data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.urllib.request.Request
的用法示例。
在下文中一共展示了Request.data方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _action
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import data [as 别名]
def _action(self, params, body=None, content_type=None):
# about token, see https://github.com/bittorrent/webui/wiki/TokenSystem
url = self.base_url + '?token=' + self.token + '&' + urlencode(params)
request = Request(url)
if body:
request.data = body
request.add_header('Content-length', len(body))
if content_type:
request.add_header('Content-type', content_type)
response = self.opener.open(request)
return response.code, json.loads(response.read())
示例2: submit
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import data [as 别名]
def submit(recaptcha_response_field,
secret_key,
remoteip,
verify_server=VERIFY_SERVER):
"""
Submits a reCAPTCHA request for verification. Returns RecaptchaResponse
for the request
recaptcha_response_field -- The value from the form
secret_key -- your reCAPTCHA secret key
remoteip -- the user's ip address
"""
if not (recaptcha_response_field and len(recaptcha_response_field)):
return RecaptchaResponse(
is_valid=False,
error_code='incorrect-captcha-sol'
)
def encode_if_necessary(s):
if isinstance(s, six.text_type):
return s.encode('utf-8')
return s
if six.PY2:
secret_key = encode_if_necessary(secret_key)
remoteip = encode_if_necessary(remoteip)
recaptcha_response_field = encode_if_necessary(
recaptcha_response_field)
params = parse.urlencode({
'secret': secret_key,
'remoteip': remoteip,
'response': recaptcha_response_field,
})
request = Request(
url="https://{0}/recaptcha/api/siteverify".format(verify_server),
data=params,
headers={
"Content-type": "application/x-www-form-urlencoded",
"User-agent": "noReCAPTCHA Python"
}
)
if six.PY3:
request.data = request.data.encode('utf-8')
httpresp = urlopen(request)
return_values = json.loads(httpresp.read())
httpresp.close()
return_code = return_values['success']
error_codes = return_values.get('error-codes', [])
if return_code:
return RecaptchaResponse(is_valid=True)
else:
return RecaptchaResponse(
is_valid=False,
error_code=error_codes
)