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


Python request.HTTPError方法代码示例

本文整理汇总了Python中urllib.request.HTTPError方法的典型用法代码示例。如果您正苦于以下问题:Python request.HTTPError方法的具体用法?Python request.HTTPError怎么用?Python request.HTTPError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib.request的用法示例。


在下文中一共展示了request.HTTPError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_access_token

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def get_access_token(self, code, state=None):
        '''
        In callback url: http://host/callback?code=123&state=xyz
        use code and state to get an access token.
        '''
        kw = dict(client_id=self._client_id, client_secret=self._client_secret, code=code)
        if self._redirect_uri:
            kw['redirect_uri'] = self._redirect_uri
        if state:
            kw['state'] = state
        opener = build_opener(HTTPSHandler)
        request = Request('https://github.com/login/oauth/access_token', data=_encode_params(kw))
        request.get_method = _METHOD_MAP['POST']
        request.add_header('Accept', 'application/json')
        try:
            response = opener.open(request, timeout=TIMEOUT)
            r = _parse_json(response.read())
            if 'error' in r:
                raise ApiAuthError(str(r.error))
            return str(r.access_token)
        except HTTPError as e:
            raise ApiAuthError('HTTPError when get access token') 
开发者ID:famavott,项目名称:osint-scraper,代码行数:24,代码来源:githubpy.py

示例2: submit_report

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def submit_report(self, report_data):
        data = json.dumps(report_data).encode('utf8')
        self.register_cbt("Logger", "LOG_DEBUG", "Usage report data: {0}".format(data))
        url = None
        try:
            url = "http://" + self._cm_config["ServerAddress"] + ":" + \
                  str(self._cm_config["ServerPort"]) + "/api/submit"
            req = urllib2.Request(url=url, data=data)
            req.add_header("Content-Type", "application/json")
            res = urllib2.urlopen(req)
            if res.getcode() == 200:
                log = "Usage report successfully submitted to server {0}\n" \
                      "HTTP response code:{1}, msg:{2}" \
                    .format(url, res.getcode(), res.read())
                self.register_cbt("Logger", "LOG_INFO", log)
            else:
                self.register_cbt("Logger", "LOG_WARNING",
                                  "Usage report server indicated error "
                                  "code: {0}".format(res.getcode()))
        except (urllib2.HTTPError, urllib2.URLError) as error:
            log = "Usage report submission failed to server {0}. " \
                  "Error: {1}".format(url, error)
            self.register_cbt("Logger", "LOG_WARNING", log) 
开发者ID:ipop-project,项目名称:Controllers,代码行数:25,代码来源:UsageReport.py

示例3: do_request

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def do_request(self, req):
        if DEBUG:
            print('requesting', req.get_method(), req.get_full_url())

        opener = self.build_opener()
        opener.add_handler(self._cookie_processor)
        try:
            self._response = opener.open(req)
        except HTTPError as e:
            self._response = e

        self.url = self._response.geturl()
        self.path = get_selector(Request(self.url))
        self.data = self._response.read()
        self.status = self._response.code
        self._forms = None
        self.form = None

        return self.get_response() 
开发者ID:Naayouu,项目名称:Hatkey,代码行数:21,代码来源:browser.py

示例4: get_status_code

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def get_status_code(url):
    """ Perform HEAD request and return status code """
    try:
        request = Request(sanitize_url(url))
        request.add_header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; "
                           "Windows NT 6.1; Trident/5.0)")
        request.get_method = lambda: 'HEAD'
        response = urlopen(request, context=ssl_unverified_context)
        # print response.info()
        return response.getcode()
    except HTTPError as e:
        return e.code
    except URLError as e:
        return e.reason
    except Exception as e:
        print(e, url)
        return None 
开发者ID:metachris,项目名称:pdfx,代码行数:19,代码来源:downloader.py

示例5: test_wrong_exception_order

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def test_wrong_exception_order(self):
        tmp = self.mkdtemp()
        path = os.path.join(tmp, 'xxx')
        self.write_file(path)
        dist_files = [('xxx', '2.6', path)]  # command, pyversion, filename
        self.write_file(self.rc, PYPIRC_LONG_PASSWORD)

        pkg_dir, dist = self.create_dist(dist_files=dist_files)
        tests = [
            (OSError('oserror'), 'oserror', OSError),
            (HTTPError('url', 400, 'httperror', {}, None),
             'Upload failed (400): httperror', DistutilsError),
        ]
        for exception, expected, raised_exception in tests:
            with self.subTest(exception=type(exception).__name__):
                with mock.patch('distutils.command.upload.urlopen',
                                new=mock.Mock(side_effect=exception)):
                    with self.assertRaises(raised_exception):
                        cmd = upload(dist)
                        cmd.ensure_finalized()
                        cmd.run()
                    results = self.get_logs(ERROR)
                    self.assertIn(expected, results[-1])
                    self.clear_logs() 
开发者ID:CedricGuillemet,项目名称:Imogen,代码行数:26,代码来源:test_upload.py

示例6: HTTPcode

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def HTTPcode(self):
		try:
			if self.agent == True:
				br = Browser()

				UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"
				header = {"User-Agent" : UserAgent}
				br.set_handle_robots(False)
				br.addheaders = [("User-agent", "Fifefox")]
				
				resp = br.open(self.target).code

			else:
				resp = u.urlopen(self.target).getcode()
	
			return(resp)
		except (u.HTTPError, u.URLError):
			return(404) 
开发者ID:fnk0c,项目名称:cangibrina,代码行数:20,代码来源:connection.py

示例7: _do_request

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def _do_request(self, data):
        """
        使用 urllib 发送数据给服务器,如果发生错误会抛出异常。
        response的结果,会返回
        """
        encoded_data = urllib.urlencode(data).encode('utf8')
        try:
            request = urllib2.Request(self._debug_url_prefix, encoded_data)
            if not self._debug_write_data:      # 说明只检查,不真正写入数据
                request.add_header('Dry-Run', 'true')
            if self._request_timeout is not None:
                response = urllib2.urlopen(request, timeout=self._request_timeout)
            else:
                response = urllib2.urlopen(request)
        except urllib2.HTTPError as e:
            return e
        return response 
开发者ID:sensorsdata,项目名称:sa-sdk-python,代码行数:19,代码来源:sdk.py

示例8: http_request

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def http_request(uri, headers=None, data=None, method=None):
        uri, headers, data, method = prepare_request(uri, headers, data,
                                                     method)

        log.debug('Request %r with %r method' % (uri, method))
        req = http.Request(uri, headers=headers, data=data)
        req.get_method = lambda: method.upper()
        try:
            resp = http.urlopen(req)
            content = resp.read()
            resp.close()
            return resp, content
        except http.HTTPError as resp:
            content = resp.read()
            resp.close()
            return resp, content 
开发者ID:gita,项目名称:BhagavadGita,代码行数:18,代码来源:client.py

示例9: download_fzf_binary

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def download_fzf_binary(plat, arch, overwrite=False, access_token=None):
    bin_path = fzf_windows_bin_path if plat == 'windows' else fzf_bin_path
    if overwrite or not os.path.isfile(bin_path):
        asset = get_fzf_binary_url(plat, arch, access_token)
        url, ext = asset
        if access_token:
            url = '{0}?access_token={1}'.format(url, access_token)
        try:
            r = urllib2.urlopen(url)
        except urllib2.HTTPError as e:
            if e.code == 403 and e.info().get('X-RateLimit-Remaining') == 0:
                raise RuntimeError(
                    'GitHub rate limit reached. To increate the limit use '
                    '-g/--github-access-token option.\n  ' + str(e)
                )
            elif e.code == 401 and access_token:
                raise RuntimeError('Invalid GitHub access token.')
            raise
        extract(r, ext, bin_path)
        r.close()
    mode = os.stat(bin_path).st_mode
    if not (mode & 0o111):
        os.chmod(bin_path, mode | 0o111) 
开发者ID:dahlia,项目名称:iterfzf,代码行数:25,代码来源:setup.py

示例10: get_access_token

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def get_access_token(self, code, state=None):
        '''
        In callback url: http://host/callback?code=123&state=xyz

        use code and state to get an access token.        
        '''
        kw = dict(client_id=self._client_id, client_secret=self._client_secret, code=code)
        if self._redirect_uri:
            kw['redirect_uri'] = self._redirect_uri
        if state:
            kw['state'] = state
        opener = build_opener(HTTPSHandler)
        request = Request('https://github.com/login/oauth/access_token', data=_encode_params(kw))
        request.get_method = _METHOD_MAP['POST']
        request.add_header('Accept', 'application/json')
        try:
            response = opener.open(request, timeout=TIMEOUT)
            r = _parse_json(response.read())
            if 'error' in r:
                raise ApiAuthError(str(r.error))
            return str(r.access_token)
        except HTTPError as e:
            raise ApiAuthError('HTTPError when get access token') 
开发者ID:vdmitriyev,项目名称:services-to-wordcloud,代码行数:25,代码来源:github.py

示例11: readSiteMap

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def readSiteMap(self):
        pages = []
        try:
            # f = urlopen("http://www.codepool.biz/sitemap.xml")
            request = build_request("http://kb.dynamsoft.com/sitemap.xml")
            f = urlopen(request, timeout=3)
            xml = f.read()

            soup = BeautifulSoup(xml)
            urlTags = soup.find_all("url")

            print "The number of url tags in sitemap: ", str(len(urlTags))

            for sitemap in urlTags:
                link = sitemap.findNext("loc").text
                pages.append(link)

            f.close()
        except HTTPError, URLError:
            print URLError.code 
开发者ID:yushulx,项目名称:crawl-404,代码行数:22,代码来源:broken_links.py

示例12: crawlLinks

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def crawlLinks(self, links, file=None):
        for link in links:
            if shutdown_event.isSet():
                return GAME_OVER

            status_code = 0

            try:
                request = build_request(link)
                f = urlopen(request)
                status_code = f.code
                f.close()
            except HTTPError, URLError:
                status_code = URLError.code

            if status_code == 404:
                if file != None:
                    file.write(link + '\n')

            print str(status_code), ':', link 
开发者ID:yushulx,项目名称:crawl-404,代码行数:22,代码来源:broken_links.py

示例13: _do_post

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def _do_post(self, url, data, headers):
        try:  # Python 3
            if data != None:
                data = bytes(data, 'utf-8')
        except:  # Python 2
            pass

        try:
            req = url_request.Request(url=url, data=data, headers=headers)
            req.add_header('User-Agent', KKBOXHTTP.USER_AGENT)
            f = url_request.urlopen(req)
            r = f.read()
        except url_request.HTTPError as e:
            print(e.fp.read())
            raise (e)
        except Exception as e:
            raise (e)

        try:  # Python 3
            r = str(r, 'utf-8')
        except:  # Python 2
            pass
            
        json_object = json.loads(r)
        return json_object 
开发者ID:KKBOX,项目名称:OpenAPI-Python,代码行数:27,代码来源:http.py

示例14: proxy_request

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def proxy_request(service_name, instance_name, path, body=None, headers=None, method='POST'):
    target = get_env("TSURU_TARGET").rstrip("/")
    token = get_env("TSURU_TOKEN")
    url = "{}/services/{}/proxy/{}?callback={}".format(target, service_name, instance_name,
                                                       path)
    request = Request(url)
    request.add_header("Authorization", "bearer " + token)
    request.get_method = lambda: method
    if body:
        try:
            request.add_data(body)
        except AttributeError:
            request.data = body.encode('utf-8')
    if headers:
        for key, value in headers.items():
            request.add_header(key, value)
    try:
        return urlopen(request)
    except HTTPError as error:
        return error
    except Exception:
        raise 
开发者ID:tsuru,项目名称:rpaas,代码行数:24,代码来源:plugin.py

示例15: request

# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPError [as 别名]
def request(self, url, data = None, options = None):
        self.response = {}
        self.response['headers'] = {}
        url = self.buildURL(url, options)
        req = Request(url, headers=self.headers)
        ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)

        try:
            self.handler = urlopen(req, data, self.timeout, context=ssl_context)
        except HTTPError as error:
            self.response['body'] = ''
            self.response['status_code'] = error.code
            return self.response

        self.response['status_code'] = self.handler.getcode()
        response_headers = dict(self.handler.info())
        if ('Content-Encoding' in response_headers and response_headers['Content-Encoding'] == 'gzip') or ('content-encoding' in response_headers and response_headers['content-encoding'] == 'gzip'):
            self.response['body'] = self.decompressBody()
        else:
            self.response['body'] = self.handler.read()

        if options and not options.get('callback') and options.get('format') == 'json':
            self.parseJsonResponse()
        else:
            self.parseRegularResponse()

        return self.response 
开发者ID:proxycrawl,项目名称:proxycrawl-python,代码行数:29,代码来源:proxycrawl_api.py


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