當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。