本文整理匯總了Python中urllib2.HTTPError方法的典型用法代碼示例。如果您正苦於以下問題:Python urllib2.HTTPError方法的具體用法?Python urllib2.HTTPError怎麽用?Python urllib2.HTTPError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib2
的用法示例。
在下文中一共展示了urllib2.HTTPError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: post_request
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def post_request(vals, url):
"""
Build a post request.
Args:
vals: Dictionary of (field, values) for the POST
request.
url: URL to send the data to.
Returns:
Dictionary of JSON response or error info.
"""
# Build the request and send to server
data = urllib.urlencode(vals)
try:
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
except urllib2.HTTPError, err:
return {"error": err.reason, "error_code": err.code}
示例2: jenkins
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def jenkins(url, port):
try:
cli_port = False
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
try:
output = urllib2.urlopen('https://'+url+':'+port+"/jenkins/", context=ctx, timeout=8).info()
cli_port = int(output['X-Jenkins-CLI-Port'])
except urllib2.HTTPError, e:
if e.getcode() == 404:
try:
output = urllib2.urlopen('https://'+url+':'+port, context=ctx, timeout=8).info()
cli_port = int(output['X-Jenkins-CLI-Port'])
except:
pass
except:
pass
示例3: gethtml
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def gethtml(url, lastURL=False):
"""return HTML of the given url"""
if not (url.startswith("http://") or url.startswith("https://")):
url = "http://" + url
header = useragents.get()
request = urllib2.Request(url, None, header)
html = None
try:
reply = urllib2.urlopen(request, timeout=10)
except urllib2.HTTPError, e:
# read html content anyway for reply with HTTP500
if e.getcode() == 500:
html = e.read()
#print >> sys.stderr, "[{}] HTTP error".format(e.code)
pass
示例4: search
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def search(self, query, pages=10):
"""search and return an array of urls"""
urls = []
try:
for url in google.search(query, start=0, stop=pages):
urls.append(url)
except HTTPError:
exit("[503] Service Unreachable")
except URLError:
exit("[504] Gateway Timeout")
except:
exit("Unknown error occurred")
else:
return urls
示例5: reverseip
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def reverseip(url):
"""return domains from given the same server"""
# get only domain name
url = urlparse(url).netloc if urlparse(url).netloc != '' else urlparse(url).path.split("/")[0]
source = "http://domains.yougetsignal.com/domains.php"
useragent = useragents.get()
contenttype = "application/x-www-form-urlencoded; charset=UTF-8"
# POST method
opener = urllib2.build_opener(
urllib2.HTTPHandler(), urllib2.HTTPSHandler())
data = urllib.urlencode([('remoteAddress', url), ('key', '')])
request = urllib2.Request(source, data)
request.add_header("Content-type", contenttype)
request.add_header("User-Agent", useragent)
try:
result = urllib2.urlopen(request).read()
except urllib2.HTTPError, e:
print >> sys.stderr, "[{}] HTTP error".format(e.code)
示例6: query
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def query(owner, name):
if fake:
print ' {0}/{1}: ok'.format(owner, name)
return (random.randint(1, 1000), random.randint(1, 300))
else:
try:
req = urllib2.Request('https://api.github.com/repos/{0}/{1}'.format(owner, name))
if user is not None and token is not None:
b64 = base64.encodestring('{0}:{1}'.format(user, token)).replace('\n', '')
req.add_header("Authorization", "Basic {0}".format(b64))
u = urllib2.urlopen(req)
j = json.load(u)
t = datetime.datetime.strptime(j['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
days = max(int((now - t).days), 0)
print ' {0}/{1}: ok'.format(owner, name)
return (int(j['stargazers_count']), days)
except urllib2.HTTPError, e:
print ' {0}/{1}: FAILED'.format(owner, name)
return (None, None)
示例7: _http_request
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def _http_request(url, headers=None, time_out=10):
"""Perform an HTTP request and return request"""
log(0, 'Request URL: {url}', url=url)
try:
if headers:
request = Request(url, headers=headers)
else:
request = Request(url)
req = urlopen(request, timeout=time_out)
log(0, 'Response code: {code}', code=req.getcode())
if 400 <= req.getcode() < 600:
raise HTTPError('HTTP %s Error for url: %s' % (req.getcode(), url), response=req)
except (HTTPError, URLError) as err:
log(2, 'Download failed with error {}'.format(err))
if yesno_dialog(localize(30004), '{line1}\n{line2}'.format(line1=localize(30063), line2=localize(30065))): # Internet down, try again?
return _http_request(url, headers, time_out)
return None
return req
示例8: query
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def query(owner, name):
if fake:
print(" {0}/{1}: ok".format(owner, name))
return (random.randint(1, 1000), random.randint(1, 300))
else:
try:
req = urllib2.Request(
"https://api.github.com/repos/{0}/{1}".format(owner, name)
)
if user is not None and token is not None:
b64 = base64.encodestring("{0}:{1}".format(user, token)).replace(
"\n", ""
)
req.add_header("Authorization", "Basic {0}".format(b64))
u = urllib2.urlopen(req)
j = json.load(u)
t = datetime.datetime.strptime(j["updated_at"], "%Y-%m-%dT%H:%M:%SZ")
days = max(int((now - t).days), 0)
print(" {0}/{1}: ok".format(owner, name))
return (int(j["stargazers_count"]), days)
except urllib2.HTTPError as e:
print(" {0}/{1}: FAILED".format(owner, name))
return (None, None)
示例9: http_download
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def http_download(url, dest):
"""Safely download url to dest.
Print error and exit if download fails.
"""
try:
print("GETting", url, "to", dest, "...", end=' ')
f = urlopen(url)
# Open our local file for writing
with open(dest, "wb") as local_file:
local_file.write(f.read())
except HTTPError as e:
print()
print("HTTP Error:", e.code, url)
sys.exit(1)
except URLError as e:
print()
print("URL Error:", e.reason, url)
sys.exit(1)
print("done.")
示例10: _make_request
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def _make_request(self, opener, request, timeout=None):
"""Make the API call and return the response. This is separated into
it's own function, so we can mock it easily for testing.
:param opener:
:type opener:
:param request: url payload to request
:type request: urllib.Request object
:param timeout: timeout value or None
:type timeout: float
:return: urllib response
"""
timeout = timeout or self.timeout
try:
return opener.open(request, timeout=timeout)
except HTTPError as err:
exc = handle_error(err)
return exc
示例11: find_cf_issues
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def find_cf_issues(domains):
error_domains = []
for domain in domains:
try:
response = urlopen('http://' + domain)
except HTTPError as e:
if e.code == 403 and 'Bad request' in e.fp.read():
try:
response = urlopen('https://' + domain)
except URLError as e:
if 'handshake' in str(e).lower() or e.code == 403 and 'Bad request' in e.fp.read():
error_domains.append(domain)
except:
pass
except:
pass
return error_domains
# add a domain to CloudFront
示例12: call_api
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def call_api(self, endpoint, method, headers=None, params=[], data=None, quiet=False):
path = self.parse_path(endpoint, params)
# If custom headers are not specified we can use the default headers
if not headers:
headers = self.headers
# Send the request and receive the response
if not self.quiet:
print('\nSending ' + method + ' request to: ' + 'https://' +self.server_ip+self.base_uri+path+'\n')
request = Request(
'https://'+self.server_ip+self.base_uri+path, headers=headers)
request.get_method = lambda: method
try:
#returns response object for opening url.
return urlopen(request, data)
except HTTPError as e:
#an object which contains information similar to a request object
return e
# This method constructs the query string
示例13: check_image_bdeps
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def check_image_bdeps(self, project, arch):
for dvd in ('000product:openSUSE-dvd5-dvd-{}'.format(arch), 'Test-DVD-{}'.format(arch)):
try:
url = makeurl(self.api.apiurl, ['build', project, 'images', arch, dvd, '_buildinfo'])
root = ET.parse(http_GET(url)).getroot()
except HTTPError as e:
if e.code == 404:
continue
raise
for bdep in root.findall('bdep'):
if 'name' not in bdep.attrib:
continue
b = bdep.attrib['name']
if b not in self.bin2src:
print("{} not found in bin2src".format(b))
continue
b = self.bin2src[b]
self.pkgdeps[b] = 'MYdvd{}'.format(self.api.rings.index(project))
break
示例14: _filter_packages_by_time
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def _filter_packages_by_time(self, packages):
x = ET.fromstring(self.cached_GET(self.makeurl(['source', self.project, '000product', '_history'], {'limit': '1'})))
producttime = int(x.find('./revision/time').text)
for pkg in packages:
try:
x = ET.fromstring(self.cached_GET(self.makeurl(['source', self.project, pkg, '_history'], {'rev': '1'})))
# catch deleted packages
except HTTPError as e:
if e.code == 404:
continue
raise e
packagetime = int(x.find('./revision/time').text)
# if producttime > packagetime:
# continue
yield pkg
示例15: add_explicit_disable
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import HTTPError [as 別名]
def add_explicit_disable(self, wipebinaries=False):
self._init_biarch_packages()
resulturl = self.makeurl(['source', self.project])
result = ET.fromstring(self.cached_GET(resulturl))
for pkg in self.packages:
changed = False
logger.debug("processing %s", pkg)
if not pkg in self.package_metas:
logger.error("%s not found", pkg)
continue
pkgmeta = self.package_metas[pkg]
build = pkgmeta.findall("./build")
if not build:
logger.debug('disable %s for %s', pkg, self.arch)
bn = pkgmeta.find('build')
if bn is None:
bn = ET.SubElement(pkgmeta, 'build')
ET.SubElement(bn, 'disable', { 'arch': self.arch })
changed = True
if changed:
try:
pkgmetaurl = self.makeurl(['source', self.project, pkg, '_meta'])
self.http_PUT(pkgmetaurl, data=ET.tostring(pkgmeta))
if self.caching:
self._invalidate__cached_GET(pkgmetaurl)
if wipebinaries:
self.http_POST(self.makeurl(['build', self.project], {
'cmd': 'wipe',
'arch': self.arch,
'package': pkg }))
except HTTPError as e:
logger.error('failed to update %s: %s', pkg, e)