当前位置: 首页>>代码示例>>Python>>正文


Python Request.timeout方法代码示例

本文整理汇总了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
开发者ID:daleathan,项目名称:weblate,代码行数:62,代码来源:saptranslationhub.py

示例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())
开发者ID:daleathan,项目名称:weblate,代码行数:14,代码来源:avatar.py

示例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)
开发者ID:daleathan,项目名称:weblate,代码行数:15,代码来源:microsoft.py

示例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
开发者ID:daleathan,项目名称:weblate,代码行数:23,代码来源:pipeline.py

示例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
开发者ID:VivekPemawat,项目名称:python-jenkins,代码行数:6,代码来源:test_kerberos.py


注:本文中的six.moves.urllib.request.Request.timeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。