本文整理汇总了Python中urllib.error方法的典型用法代码示例。如果您正苦于以下问题:Python urllib.error方法的具体用法?Python urllib.error怎么用?Python urllib.error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib
的用法示例。
在下文中一共展示了urllib.error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ExecuteDelete_JSON
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def ExecuteDelete_JSON(session_id, uri, sub_url, timeout):
headers = CreateHeadersWithSessionCookieAndCustomHeader(session_id)
uri = uri + sub_url
try:
ExecuteWebRequest(uri, None, headers, timeout, lambda: 'DELETE')
except urllib.error.HTTPError as e:
if e.code == 410:
return True
raise e
try:
ExecuteGet_JSON(session_id, uri, sub_url, timeout)
except urllib.error.HTTPError as e:
if e.code == 404:
return True
raise e
return False
示例2: Open
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def Open(self):
"""
Opens a session to the nexpose appliance by logging in.
This function with raise an exception on error or if the session is already open.
"""
if self._session_id:
raise SessionIsNotClosedException("Please close the session first!")
try:
response = self._Execute_APIv1d1(self._login_request)
except NexposeConnectionException as ex:
if isinstance(ex.inner_exception, etree.XMLSyntaxError):
raise NexposeException("Unexpected error! Is the Nexpose appliance activated?")
raise ex
if response.tag == "LoginResponse":
if response.attrib["success"] == "1":
self._session_id = response.attrib["session-id"]
if not self._session_id:
raise NexposeFailureException("Login failure!")
示例3: VerifySuccess
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def VerifySuccess(self, response):
"""
If the response indicates a failure, the error message is extracted and
a NexposeFailureException is raised. Otherwise the response is returned.
"""
if response.tag == 'Failure':
message = get_content_of(response, 'Exception/Message')
if message is None:
message = get_content_of(response, 'Message')
raise NexposeFailureException(message)
if get_attribute(response, 'success') == '0':
message = get_content_of(response, 'Failure/message')
if message is None:
message = get_content_of(response, 'Failure/Exception/message')
if message is None:
message = get_content_of(response, 'Error') # Used by unofficial API's (for example: TestAdminCredentialsResult)
raise NexposeFailureException(message)
return response
#
# The following functions implement the Site Management API:
# =========================================================
示例4: DeleteSharedCredential
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def DeleteSharedCredential(self, id_or_shared_credential):
"""
Delete a shared credential from the security console.
An id or dl_nexpose.SharedCredential object is expected.
This function will return a boolean indicating success.
NOTE: no official documentation exists except the Rapid7 Nexpose Ruby API.
"""
# TODO: To be equal with the other delete functions, this one should raise an exception
try:
if isinstance(id_or_shared_credential, SharedCredentialBase):
id_or_shared_credential = id_or_shared_credential.id
return self.ExecuteFormPost('data/credential/shared/delete?credid={0}'.format(id_or_shared_credential), '') == 'true'
except urllib.error.HTTPError:
return False
#
# The following functions implement the Backup Management API (not documented):
# ===========================================================
示例5: _check_cert
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def _check_cert(filename):
"""
Does this certificate file look okay?
Returns error message, or None if okay
"""
try:
st = os.stat(filename)
except OSError:
return filename + " doesn't exist"
else:
good_perm = stat.S_IFREG | stat.S_IRUSR # | stat.S_IWUSR
if (st[stat.ST_UID], st[stat.ST_GID]) != (0,0):
return 'not owned by root.root'
perm = st[stat.ST_MODE]
if good_perm != perm:
return "expected permissions %o but found %o." % (good_perm, perm)
示例6: _check_file_create
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def _check_file_create(directory):
"""
Check that files can be created in the given directory.
Returns error message, or None if okay
"""
filename = os.path.join(directory, 'filewrite-' + str(os.getpid()) + '.tmp')
if not os.path.isdir(directory):
return 'directory does not exist'
try:
fh = open(filename, 'w')
except IOError:
return 'could not write to a file'
else:
fh.write('test file: may safely delete')
fh.close()
os.unlink(filename)
示例7: acad_plan_count
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def acad_plan_count(acad_plan, strm):
"""
Return number of majors in academic plan (e.g. 'CMPTMAJ') in the semester or a SIMSProblem instance (error message).
"""
db = SIMSConn()
# most recent acad_plan, most recent acad_plan *in this program* for each student active this semester
last_prog_plan = "(SELECT ap.emplid, max(ap.effdt) as effdtprog, max(ap2.effdt) as effdt " \
"FROM ps_acad_plan ap, ps_acad_plan ap2, ps_stdnt_car_term ct "\
"WHERE ap.emplid=ct.emplid and ap.emplid=ap2.emplid and ct.strm=%s and ap.acad_plan=%s " \
"and ct.stdnt_car_nbr=ap.stdnt_car_nbr GROUP BY ap.emplid)"
# select those whose most recent program is in this acad_plan
db.execute("SELECT count(*) FROM " + last_prog_plan + " WHERE effdtprog=effdt", (strm, acad_plan))
return db.fetchone()[0]
#for row in db:
# print row
示例8: outlines_data_json
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def outlines_data_json(offering):
url = outlines_api_url(offering)
try:
req = urllib.request.urlopen(url, timeout=30)
jsondata = req.read()
data = json.loads(jsondata.decode('utf8'))
except ValueError:
data = {'internal_error': 'could not decode JSON'}
except (urllib.error.HTTPError, urllib.error.URLError, socket.timeout, socket.error):
data = {'internal_error': 'could not retrieve outline data from API'}
if 'info' in data and 'outlinePath' in data['info']:
data['outlineurl'] = OUTLINES_FRONTEND_BASE + '?' + data['info']['outlinePath']
return json.dumps(data, indent=1)
##############################################################################
# Emplid -> Userid APIs
示例9: userid_to_emplid
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def userid_to_emplid(userid):
"""
Fetch emplid from ITS API for userid -> emplid mapping.
Admin contact for the API is George Lee in the Learning & Community Platforms Group
"""
qs = urllib.parse.urlencode({'art': EMPLID_SECRET, 'username': userid})
url = EMPLID_BASE_URL + qs
try:
req = urllib.request.urlopen(url, timeout=30)
jsondata = req.read().decode('utf8')
data = json.loads(jsondata)
except ValueError:
# can't decode JSON
return None
except (urllib.error.HTTPError, urllib.error.URLError, http.client.HTTPException, socket.timeout, socket.error):
# network problem, or 404 (if userid doesn't exist)
return None
if 'sfuid' not in data:
return None
return data['sfuid']
示例10: emplid_to_userid
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def emplid_to_userid(emplid):
"""
Fetch userid from ITS API for emplid -> userid mapping.
Admin contact for the API is George Lee in the Learning & Community Platforms Group
"""
qs = urllib.parse.urlencode({'art': EMPLID_SECRET, 'sfuid': str(emplid)})
url = USERID_BASE_URL + qs
try:
req = urllib.request.urlopen(url, timeout=30)
jsondata = req.read().decode('utf8')
data = json.loads(jsondata)
except ValueError:
# can't decode JSON
return None
except (urllib.error.HTTPError, urllib.error.URLError, http.client.HTTPException):
# network problem, or 404 (if userid doesn't exist)
return None
if 'username' not in data:
raise ValueError("No 'username' returned in response.")
userids = data['username']
return userids.split(',')[0].strip()
示例11: calculate_all
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def calculate_all(request, course_slug, activity_slug):
course = get_object_or_404(CourseOffering, slug=course_slug)
activity = get_object_or_404(CalNumericActivity, slug=activity_slug, offering=course, deleted=False)
try:
ignored, hiding_info = calculate_numeric_grade(course,activity)
if hiding_info:
messages.warning(request, "This activity is released to students, but the calculation uses unreleased grades. Calculations done with unreleased activities as zero to prevent leaking hidden info to students.")
if ignored==1:
messages.warning(request, "Did not calculate grade for 1 manually-graded student.")
elif ignored>1:
messages.warning(request, "Did not calculate grade for %i manually-graded students." % (ignored))
except ValidationError as e:
messages.error(request, e.args[0])
except EvalException as e:
messages.error(request, e.args[0])
except NotImplementedError:
return NotFoundResponse(request)
return HttpResponseRedirect(activity.get_absolute_url())
示例12: calculate_all_lettergrades
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def calculate_all_lettergrades(request, course_slug, activity_slug):
course = get_object_or_404(CourseOffering, slug=course_slug)
activity = get_object_or_404(CalLetterActivity, slug=activity_slug, offering=course, deleted=False)
try:
ignored = calculate_letter_grade(course,activity)
if ignored==1:
messages.warning(request, "Did not calculate letter grade for 1 manually-graded student.")
elif ignored>1:
messages.warning(request, "Did not calculate letter grade for %i manually-graded students." % (ignored))
except ValidationError as e:
messages.error(request, e.args[0])
except NotImplementedError:
return NotFoundResponse(request)
return HttpResponseRedirect(activity.get_absolute_url())
示例13: regex
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def regex(ioc_type):
ioc_patts = {
"ip":b"((?:(?:[12]\d?\d?|[1-9]\d|[1-9])(?:\[\.\]|\.)){3}(?:[12]\d?\d?|[\d+]{1,2}))",
"domain":b"([A-Za-z0-9]+(?:[\-|\.][A-Za-z0-9]+)*(?:\[\.\]|\.)(?:com|net|edu|ru|org|de|uk|jp|br|pl|info|fr|it|cn|in|su|pw|biz|co|eu|nl|kr|me))",
"md5":b"\W([A-Fa-f0-9]{32})(?:\W|$)",
"sha1":b"\W([A-Fa-f0-9]{40})(?:\W|$)",
"sha256":b"\W([A-Fa-f0-9]{64})(?:\W|$)",
"email":b"[a-zA-Z0-9_]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?!([a-zA-Z0-9]*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*\.))(?:[A-Za-z0-9](?:[a-zA-Z0-9-]*[A-Za-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?",
"URL":b"((?:http|ftp|https)\:\/\/(?:[\w+?\.\w+])+[a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;]+)",
"yara":b"(rule\s[\w\W]{,30}\{[\w\W\s]*\})"
}
try:
pattern = re.compile(ioc_patts[ioc_type])
except re.error:
print('[!] Invalid type specified.')
sys.exit(0)
return pattern
示例14: update_progress
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def update_progress(progress):
barLength = 20 # Modify this value to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = Fore.RED + "Halt!\r\n"
if progress >= .999:
progress = 1
status = Fore.GREEN + " Complete!\r\n"
block = int(round(barLength*progress))
text = "\r[*] Progress: [{0}] {1}% {2}".format("#"*block + "-"*(barLength-block), round(progress*100), status)
sys.stdout.write(text)
sys.stdout.flush()
示例15: get_web_page
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import error [as 别名]
def get_web_page(url, headers, cookies):
try:
logging.info(f'Fetching {url}')
request = urllib.request.Request(url, None, headers)
request.add_header('Authorization', cookies)
response = urllib.request.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
buf = BytesIO(response.read())
f = gzip.GzipFile(fileobj=buf)
r = f.read()
else:
r = response.read()
return r
except urllib.error.HTTPError as e:
logging.info(f"Error processing webpage: {e}")
if e.code == ALREADY_CLICKED_CODE:
return ALREADY_CLICKED_CODE
if e.code == UNAUTHORIZED:
return UNAUTHORIZED
return None