本文整理汇总了Python中six.moves.urllib.request.Request.timeout方法的典型用法代码示例。如果您正苦于以下问题:Python Request.timeout方法的具体用法?Python Request.timeout怎么用?Python Request.timeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.urllib.request.Request
的用法示例。
在下文中一共展示了Request.timeout方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_translations
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import timeout [as 别名]
def download_translations(self, source, language, text, unit, user):
"""Download list of possible translations from a service."""
# should the machine translation service be used?
# (rather than only the term database)
enable_mt = False
if isinstance(settings.MT_SAP_USE_MT, bool):
enable_mt = settings.MT_SAP_USE_MT
# build the json body
request_data_as_bytes = json.dumps(
{
'targetLanguages': [language],
'sourceLanguage': source,
'enableMT': enable_mt,
'enableTranslationQualityEstimation': enable_mt,
'units': [{'value': text}]
},
ensure_ascii=False
).encode('utf-8')
# create the request
translation_url = settings.MT_SAP_BASE_URL + 'translate'
request = Request(
translation_url if six.PY3 else translation_url.encode("utf-8")
)
request.timeout = 0.5
request.add_header('User-Agent', USER_AGENT.encode('utf-8'))
request.add_header('Referer', get_site_url().encode('utf-8'))
request.add_header('Content-Type', 'application/json; charset=utf-8')
request.add_header('Content-Length', len(request_data_as_bytes))
request.add_header('Accept', 'application/json; charset=utf-8')
self.authenticate(request)
# Read and possibly convert response
content = urlopen(
request, request_data_as_bytes
).read().decode('utf-8')
# Replace literal \t
content = content.strip().replace(
'\t', '\\t'
).replace(
'\r', '\\r'
)
response = json.loads(content)
translations = []
# prepare the translations for weblate
for item in response['units']:
for translation in item['translations']:
translations.append((
translation['value'],
translation.get('qualityIndex', 100),
self.name,
text
))
return translations
示例2: download_avatar_image
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import timeout [as 别名]
def download_avatar_image(user, size):
"""Download avatar image from remote server."""
url = avatar_for_email(user.email, size)
request = Request(url)
request.timeout = 0.5
request.add_header('User-Agent', USER_AGENT)
# Fire request
handle = urlopen(request)
# Read and possibly convert response
return bytes(handle.read())
示例3: soap_req
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import timeout [as 别名]
def soap_req(self, action, **kwargs):
template = get_template(
'machine/microsoft_terminology_{}.xml'.format(action.lower())
)
payload = template.render(kwargs)
request = Request(self.MS_TM_API_URL, payload.encode('utf-8'))
request.timeout = 0.5
request.add_header(
'SOAPAction', '"{}"'.format(self.MS_TM_SOAP_HEADER + action)
)
request.add_header('Content-Type', 'text/xml; charset=utf-8')
return urlopen(request)
示例4: get_github_email
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import timeout [as 别名]
def get_github_email(access_token):
"""Get real email from GitHub"""
request = Request('https://api.github.com/user/emails')
request.timeout = 1.0
request.add_header('User-Agent', USER_AGENT)
request.add_header(
'Authorization',
'token {0}'.format(access_token)
)
handle = urlopen(request)
data = json.loads(handle.read().decode('utf-8'))
email = None
for entry in data:
# Skip not verified ones
if not entry['verified']:
continue
email = entry['email']
if entry['primary']:
break
return email
示例5: _get_dummy_request
# 需要导入模块: from six.moves.urllib.request import Request [as 别名]
# 或者: from six.moves.urllib.request.Request import timeout [as 别名]
def _get_dummy_request(self):
r = Request('http://example.com')
r.timeout = 10
return r