本文整理汇总了Python中urllib.request.URLError方法的典型用法代码示例。如果您正苦于以下问题:Python request.URLError方法的具体用法?Python request.URLError怎么用?Python request.URLError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request
的用法示例。
在下文中一共展示了request.URLError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_url_nodecode
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request 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
示例2: http_pull_file
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def http_pull_file(remote_file,remote_mtime,local_file,MODE):
#-- Printing files transferred
print('{0} -->\n\t{1}\n'.format(remote_file,local_file))
#-- Create and submit request. There are a wide range of exceptions
#-- that can be thrown here, including HTTPError and URLError.
request = urllib2.Request(remote_file)
response = urllib2.urlopen(request)
#-- chunked transfer encoding size
CHUNK = 16 * 1024
#-- copy contents to local file using chunked transfer encoding
#-- transfer should work properly with ascii and binary data formats
with open(local_file, 'wb') as f:
shutil.copyfileobj(response, f, CHUNK)
#-- keep remote modification time of file and local access time
os.utime(local_file, (os.stat(local_file).st_atime, remote_mtime))
os.chmod(local_file, MODE)
#-- PURPOSE: help module to describe the optional input parameters
示例3: submit_report
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [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)
示例4: check_backend
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def check_backend(backend):
"""Check whether have pre-tuned parameters of the certain target.
If not, will download it.
Parameters
----------
backend: str
The name of backend.
"""
backend = _alias(backend)
assert backend in PACKAGE_VERSION, 'Cannot find backend "%s" in TopHub' % backend
version = PACKAGE_VERSION[backend]
package_name = "%s_%s.log" % (backend, version)
if os.path.isfile(os.path.join(AUTOTVM_TOPHUB_ROOT_PATH, package_name)):
return
if sys.version_info >= (3,):
import urllib.request as urllib2
else:
import urllib2
try:
download_package(package_name)
except urllib2.URLError as e:
logging.warning("Failed to download tophub package for %s: %s", backend, e)
示例5: get_status_code
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [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
示例6: test_non_existing_host
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def test_non_existing_host(self):
Profile = osrm.RequestConfig("localhost/v1/flying")
self.assertEqual(Profile.host, "localhost")
with self.assertRaises(URLError):
osrm.nearest((12.36, 45.36), url_config=Profile)
with self.assertRaises(URLError):
osrm.trip(
[(13.38886, 52.51703), (10.00, 53.55), (52.374444, 9.738611)],
url_config=Profile)
with self.assertRaises(URLError):
osrm.simple_route(
(13.38886, 52.51703), (10.00, 53.55), url_config=Profile)
with self.assertRaises(URLError):
osrm.AccessIsochrone(
(13.38886, 52.51703), points_grid=100, url_config=Profile)
with self.assertRaises(URLError):
osrm.match(
[(10.00, 53.55), (52.374444, 9.738611)], url_config=Profile)
with self.assertRaises(URLError):
osrm.table(
[(10.00, 53.55), (52.374444, 9.738611)],
[(10.00, 53.55), (52.374444, 9.738611)],
url_config=Profile)
示例7: HTTPcode
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [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)
示例8: write_to_influx_db
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def write_to_influx_db(url, db_name, summaries):
"""Write the extracted summaries to influxdb."""
metrics = []
target = url + '/write?db=%s' % db_name
for suite in summaries:
common_tags = {'suite': suite['suite']}
common_tags.update(suite.get('config', {}))
for test in suite['tests']:
common_tags['test'] = test['name']
for attempt in test['attempts']:
metrics.extend(encode_attempt_metrics(attempt, common_tags))
payload = '\n'.join(metrics)
req = Request(url=target, data=payload)
req.get_method = lambda: 'POST'
try:
urlopen(req)
print('WROTE %d metrics to %s' % (len(metrics), target))
except URLError as err:
print('ERROR: %s\n%s' % (err.reason, err.read()))
raise
示例9: mm_heartbeat
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def mm_heartbeat(self):
# Check if stop or set next timer
if self.shutdown:
return
threading.Timer(self.hb_timer, self.mm_heartbeat).start()
address = ("http://" + self.mm_host + ":" + self.mm_port + "/alexapi?action=AVSHB")
logger.debug("Sending MM Heatbeat")
try:
response = urlopen(address).read()
except URLError as err:
logger.error("URLError: %s", err.reason)
return
logger.debug("Response: %s", response)
示例10: get_playlist
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def get_playlist(self):
if stdin_args.playlist:
self.json_file = stdin_args.playlist
else:
year, month, day = self.list_date.split('-')
self.json_file = os.path.join(
_playlist.path, year, month, self.list_date + '.json')
if '://' in self.json_file:
self.json_file = self.json_file.replace('\\', '/')
try:
req = request.urlopen(self.json_file,
timeout=1,
context=ssl._create_unverified_context())
b_time = req.headers['last-modified']
temp_time = time.strptime(b_time, "%a, %d %b %Y %H:%M:%S %Z")
mod_time = time.mktime(temp_time)
if mod_time > self.last_mod_time:
self.clip_nodes = valid_json(req)
self.last_mod_time = mod_time
messenger.info('Open: ' + self.json_file)
validate_thread(self.clip_nodes)
except (request.URLError, socket.timeout):
self.eof_handling('Get playlist from url failed!', False)
elif os.path.isfile(self.json_file):
# check last modification from playlist
mod_time = os.path.getmtime(self.json_file)
if mod_time > self.last_mod_time:
with open(self.json_file, 'r', encoding='utf-8') as f:
self.clip_nodes = valid_json(f)
self.last_mod_time = mod_time
messenger.info('Open: ' + self.json_file)
validate_thread(self.clip_nodes)
else:
self.clip_nodes = None
示例11: download_image_unsplash_random
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def download_image_unsplash_random(download_dir, image_extension="jpg"):
"""
Download & save the image
:param download_dir: directory where to download the image
:param image_extension: directory where to download the image
:return: downloaded image path
"""
url = "https://source.unsplash.com/random/1920x1080"
try:
image_url = url
image_name = "unsplash_random"
date_time = datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
image_file_name = "{image_name}_{date_stamp}.{extention}".format(
image_name=image_name, date_stamp=date_time, extention=image_extension
)
image_path = os.path.join(os.sep, download_dir, image_file_name)
log.debug("download_dir: {}".format(download_dir))
log.debug("image_file_name: {}".format(image_file_name))
log.debug("image_path: {}".format(image_path))
if os.path.isfile(image_path):
log.info("No new wallpaper yet..updating to latest one.\n")
return image_path
log.info("Downloading...")
urlretrieve(image_url, filename=image_path)
return image_path
except URLError:
log.error("Something went wrong..\nMaybe Internet is not working...")
raise ConnectionError
示例12: host_is_up
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def host_is_up(hostname, port, protocol='http'):
url = f'{protocol}://{hostname}:{port}'
try:
request.urlopen(url).getcode()
return True
except request.URLError:
return False
示例13: request
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def request(self, host, handler, request_body, verbose=0):
"""Send XMLRPC request"""
uri = '{scheme}://{host}{handler}'.format(scheme=self._scheme,
host=host, handler=handler)
if self._passmgr:
self._passmgr.add_password(None, uri, self._username,
self._password)
if self.verbose:
_LOGGER.debug("FabricTransport: {0}".format(uri))
opener = urllib2.build_opener(*self._handlers)
headers = {
'Content-Type': 'text/xml',
'User-Agent': self.user_agent,
}
req = urllib2.Request(uri, request_body, headers=headers)
try:
return self.parse_response(opener.open(req))
except (urllib2.URLError, urllib2.HTTPError) as exc:
try:
code = -1
if exc.code == 400:
reason = 'Permission denied'
code = exc.code
else:
reason = exc.reason
msg = "{reason} ({code})".format(reason=reason, code=code)
except AttributeError:
if 'SSL' in str(exc):
msg = "SSL error"
else:
msg = str(exc)
raise InterfaceError("Connection with Fabric failed: " + msg)
except BadStatusLine:
raise InterfaceError("Connection with Fabric failed: check SSL")
示例14: goglib_get_banner
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def goglib_get_banner(banner_path, unavailable_path, game_id, *args):
banner_height = 240
game_name = os.path.basename(banner_path).split('.jpg')[0]
print("Getting picture for: '" + game_name + "'")
try:
token_path = os.getenv('HOME') + '/.config/lgogdownloader/galaxy_tokens.json'
token = Token.from_file(token_path)
if token.expired():
token.refresh()
token.save(token_path)
api = GogApi(token)
prod = api.product(game_id)
prod.update_galaxy(expand=True)
banner_url = 'https:' + ''.join(prod.image_logo.split('_glx_logo'))
banner_req = urllib_request(banner_url)
banner_data = urllib_urlopen(banner_req).read()
banner_file = open(banner_path, 'wb')
banner_file.write(banner_data)
banner_file.close()
pic_src = Image.open(banner_path)
scale_lvl = banner_height/float(pic_src.size[1])
scaled_width = int(float(pic_src.size[0])*scale_lvl)
pic = pic_src.resize((scaled_width, banner_height), PIL.Image.ANTIALIAS)
pic.save(banner_path)
pic = pic.convert('L')
pic.save(unavailable_path)
except urllib_urlerror as e:
print(e.reason)
except urllib_httperror as e:
print(e.code)
print(e.read())
except:
goglib_recreate_banner.goglib_recreate_banner(game_name, banner_path)
示例15: get_banner
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import URLError [as 别名]
def get_banner(game_name, url, banner_path, lib):
banner_req = urllib_request(url)
try:
if not os.path.exists(banner_path):
os.makedirs(banner_path)
banner_data = urllib_urlopen(banner_req).read()
banner_file = open(banner_path + '/' + game_name + '.jpg', 'wb')
banner_file.write(banner_data)
banner_file.close()
pic_src = Image.open(banner_path + '/' + game_name + '.jpg')
pic = pic_src.resize((518, 240), PIL.Image.ANTIALIAS)
pic.save(banner_path + '/' + game_name + '.jpg')
if lib == 'goglib':
if not os.path.exists(banner_path + '/unavailable/'):
os.makedirs(banner_path + '/unavailable/')
new_pic = Image.open(banner_path + '/' + game_name + '.jpg')
pic_grey = new_pic.convert('L')
pic_grey.save(banner_path + '/unavailable/' + game_name + '.jpg')
except urllib_urlerror as e:
print(e.reason)
except urllib_httperror as e:
print(e.code)
print(e.read())