本文整理匯總了Python中urllib2.URLError方法的典型用法代碼示例。如果您正苦於以下問題:Python urllib2.URLError方法的具體用法?Python urllib2.URLError怎麽用?Python urllib2.URLError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib2
的用法示例。
在下文中一共展示了urllib2.URLError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: search
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [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
示例2: DownloadFile
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def DownloadFile(self, fileurl, dlfile):
"""Downloads a given file to a given path/filename.
Args:
fileurl: String with URL of file to download.
dlfile: String with path of file to be written to.
Raises:
OSError: If file cannot be opened/written to, function raises OSError.
URLError: If URL cannot be opened, fucntion raises URLError.
"""
if not os.path.isfile(dlfile) or dlfile == TMPINDEX:
print 'Downloading %s ...' % fileurl
file_to_dl = urllib2.urlopen(fileurl)
tmpfile = open(dlfile, 'wb')
shutil.copyfileobj(file_to_dl, tmpfile)
else:
print '%s exists' % dlfile
示例3: _http_request
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [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
示例4: _checkout
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def _checkout(self, local_dir):
user = self.config.get("user")
repo = self.config.get("repo")
version = self.config.get("version", "master")
# TODO : Sanitize URL
url = URL.format(user=user, repo=repo, version=version)
logger.info("Downloading {}/{} from github".format(user, repo))
try:
(filename, headers) = urllib.urlretrieve(url)
except URLError as e:
raise RuntimeError("Failed to download '{}'. '{}'".format(url, e.reason))
t = tarfile.open(filename)
(cache_root, core) = os.path.split(local_dir)
# Ugly hack to get the first part of the directory name of the extracted files
tmp = t.getnames()[0]
t.extractall(cache_root)
os.rename(os.path.join(cache_root, tmp), os.path.join(cache_root, core))
示例5: validate_
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def validate_(self, value, context=None):
url = self.valid_url(value)
if not url:
raise StopValidationError(self.messages['invalid_url'])
if self.verify_exists:
url_string = urlquote(urlunsplit((
url['scheme'],
(url['host6'] or url['host4'] or url['hostn_enc']) + ':' + (url['port'] or ''),
url['path'],
url['query'],
url['frag'])
).encode('utf-8'), safe=VALID_CHAR_STRING)
try:
urlopen(url_string)
except URLError:
raise StopValidationError(self.messages['not_found'])
示例6: http_download
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [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.")
示例7: get_cf_ranges
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def get_cf_ranges(cf_url):
response = None
ranges = []
while response is None:
try:
response = urlopen(cf_url)
except URLError as e:
print(' [?] Got URLError trying to get CloudFront IP ranges. Retrying...')
except:
print(' [?] Got an unexpected error trying to get CloudFront IP ranges. Exiting...')
raise
cf_data = json.load(response)
for item in cf_data['prefixes']:
service = item.get('service')
if service == 'CLOUDFRONT':
ranges.append(item.get('ip_prefix'))
return ranges
# find more domains and correct for CloudFront
示例8: find_cf_issues
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [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
示例9: retrieve_url_nodecode
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def retrieve_url_nodecode(url):
""" Return the content of the url page as a string """
req = Request(url, headers=headers)
try:
response = urlopen(req)
except URLError as errno:
print(" ".join(("Connection error:", str(errno.reason))))
print(" ".join(("URL:", url)))
return ""
dat = response.read()
# Check if it is gzipped
if dat[:2] == '\037\213':
# Data is gzip encoded, decode it
compressedstream = StringIO(dat)
gzipper = gzip.GzipFile(fileobj=compressedstream)
extracted_data = gzipper.read()
dat = extracted_data
return dat
return dat
示例10: Quit
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def Quit(self, timeout=5.0):
"""Causes the API Server process to exit.
Args:
timeout: The maximum number of seconds to wait for an orderly shutdown
before forceably killing the process.
"""
assert self._process, 'server was not started'
if self._process.poll() is None:
try:
urllib2.urlopen(self.url + QUIT_PATH)
except urllib2.URLError:
pass
finish_time = time.time() + timeout
while time.time() < finish_time and self._process.poll() is None:
time.sleep(0.2)
if self._process.returncode is None:
logging.warning('api_server did not quit cleanly, killing')
self._process.kill()
示例11: check_for_update
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def check_for_update():
if os.path.exists(FILE_UPDATE):
mtime = os.path.getmtime(FILE_UPDATE)
last = datetime.utcfromtimestamp(mtime).strftime('%Y-%m-%d')
today = datetime.utcnow().strftime('%Y-%m-%d')
if last == today:
return
try:
with open(FILE_UPDATE, 'a'):
os.utime(FILE_UPDATE, None)
request = urllib2.Request(
CORE_VERSION_URL,
urllib.urlencode({'version': __version__}),
)
response = urllib2.urlopen(request)
with open(FILE_UPDATE, 'w') as update_json:
update_json.write(response.read())
except (urllib2.HTTPError, urllib2.URLError):
pass
示例12: postRequest
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def postRequest(self, url, params):
params = urlencode(params)
headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"}
req = urllib2.Request(url=url, data=params, headers=headers)
try:
response = urllib2.urlopen(req)
log.debug("%s?%s" % (response.geturl(), params))
return json.loads(response.read())
except urllib2.URLError, e:
log.debug(e)
if hasattr(ssl, '_create_unverified_context'): #for mac os only in order to ignore invalid certificates
try:
context = ssl._create_unverified_context()
response = urllib2.urlopen(req, context=context)
return json.loads(response.read())
except Exception, e:
log.exception(e)
示例13: do_scan
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def do_scan(crawling):
while 1:
try:
crawling = tocrawl.pop()
# print crawling
except KeyError:
sys.exit(1)
url = urlparse.urlparse(crawling)
try:
response = urllib2.urlopen(crawling)
except urllib2.HTTPError, e:
continue
except urllib2.URLError, e:
log_file = "sqli.txt"
FILE = open(log_file, "a")
FILE.write(crawling)
FILE.close()
print "\n================================================================================"
print "\t\tBlind MySQL Injection Detected"
print crawling
print "\n===============================================================================\n"
winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)
time.sleep(10)
continue
示例14: http_request
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def http_request(url,
data=None,
method='PUT',
headers=None,
timeout=None,
should_fail=False):
headers = headers or {}
request = urllib2.Request(url, data=data, headers=headers)
request.get_method = lambda: method
try:
if timeout:
return urllib2.urlopen(request, timeout=timeout)
return urllib2.urlopen(request)
except urllib2.URLError as e:
if not should_fail:
ctx.logger.error('Failed to {0} {1} (reason: {2})'.format(
method, url, e.reason))
示例15: __send
# 需要導入模塊: import urllib2 [as 別名]
# 或者: from urllib2 import URLError [as 別名]
def __send(self, uri, data):
req = urllib2.Request(uri)
req.add_header('Content-Type', 'application/json')
event = None
try:
urllib2.urlopen(req, data)
except ValueError as error:
event = Event(Events.PUSH_ERROR, msg=error.message)
except URLError as error:
event = Event(Events.PUSH_ERROR, msg=error.reason.strerror)
if event is not None:
self._failed.append(data)
post_event(self._handler, event)