python进阶(8):常用模块2+异常处理
来源:互联网 发布:sqlserver 字符串截取 编辑:程序博客网 时间:2024/05/21 10:09
前段时间讲了很多的模块应为当时面向对象没有讲有几个没有说今天补上,再说一个异常处理。
一、hashlib模块
Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等。
摘要算法又称哈希算法、散列算法。它通过一个函数,把任意长度的数据转换为一个长度固定的数据串
import hashlib md5 = hashlib.md5()md5.update('how to use md5 in python hashlib?'.'utf-8')print(md5.hexdigest())计算结果如下:d26a53750bc40b38b65a520292f69306
如果数据量很大,可以分块多次调用update(),最后计算的结果是一样的:
md5 = hashlib.md5()md5.update('how to use md5 in '.'utf-8')md5.update('python hashlib?'.'utf-8')print(md5.hexdigest())
MD5是最常见的摘要算法,速度很快,生成结果是固定的128 bit字节,通常用一个32位的16进制字符串表示。另一种常见的摘要算法是SHA1,调用SHA1和调用MD5完全类似:
import hashlib sha1 = hashlib.sha1()sha1.update('how to use sha1 in python hashlib?'.'utf-8')print sha1.hexdigest()
SHA1的结果是160 bit字节,通常用一个40位的16进制字符串表示。比SHA1更安全的算法是SHA256和SHA512,不过越安全的算法越慢,而且摘要长度更长。
摘要算法的应用:
任何允许用户登录的网站都会存储用户登录的用户名和口令。会将用户名和口令存在数据库中。如果以明文保存用户口令,如果数据库泄露,所有用户的口令就落入黑客的手里。此外,网站运维人员是可以访问数据库的,也就是能获取到所有用户的口令。正确的保存口令的方式是不存储用户的明文口令,而是存储用户口令的摘要,比如MD5
import hashlibuser = 'lln'pwd = '110'md5_obj = hashlib.md5()md5_obj.update(pwd.encode('utf-8'))print(md5_obj.hexdigest())
因为摘要函数是一个单向函数,无法通过摘要反破译出明文,那么很多的破译工具是依据什么破译的呢?很多用户喜欢用123456,888888,password这些简单的口令,于是,黑客可以事先计算出这些常用口令的MD5值,得到一个反推表,用你的摘要一一匹配俗称撞库。
由于常用口令的MD5值很容易被计算出来,所以,要确保存储的用户口令不是那些已经被计算出来的常用口令的MD5,这一方法通过对原始口令加一个复杂字符串来实现,俗称“加盐”:
import hashlibuser = 'lln'pwd = '110'md5_obj = hashlib.md5(user.encode('utf-8')) #加盐md5_obj.update(pwd.encode('utf-8'))print(md5_obj.hexdigest())
摘要算法在很多地方都有广泛的应用。要注意摘要算法不是加密算法,不能用于加密(因为无法通过摘要反推明文),只能用于防篡改,但是它的单向计算特性决定了可以在不存储明文口令的情况下验证用户口令。
二、configparser模块
该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
1、创建配置文件
[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes [bitbucket.org]User = hg [topsecret.server.com]Port = 50022ForwardX11 = no
import configparserconfig = configparser.ConfigParser()config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9', 'ForwardX11':'yes' }config['bitbucket.org'] = {'User':'hg'}config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}with open('example.ini', 'w') as configfile: config.write(configfile)
2、查找文件
import configparserconfig = configparser.ConfigParser()#---------------------------查找文件内容,基于字典的形式print(config.sections()) # []config.read('example.ini')print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']print('bytebong.com' in config) # Falseprint('bitbucket.org' in config) # Trueprint(config['bitbucket.org']["user"]) # hgprint(config['DEFAULT']['Compression']) #yesprint(config['topsecret.server.com']['ForwardX11']) #noprint(config['bitbucket.org']) #<Section: bitbucket.org>for key in config['bitbucket.org']: # 注意,有default会默认default的键 print(key)print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value
3、增删改操作
import configparserconfig = configparser.ConfigParser()config.read('example.ini')config.add_section('yuan')config.remove_section('bitbucket.org')config.remove_option('topsecret.server.com',"forwardx11")config.set('topsecret.server.com','k1','11111')config.set('yuan','k2','22222')config.write(open('new2.ini', "w"))
三、logging模块
明日更新。。。
四、异常处理
1、异常种类
x #NameError: name 'x' is not definedint('abc') #ValueError: invalid literal for int() with base 10: 'abc'l = []l[10000] #IndexError: list index out of rangeclass Foo:passFoo.x #AttributeError: type object 'Foo' has no attribute 'x'1/0 #ZeroDivisionError: division by zerodic = {'k':'v'}dic['k2'] #KeyError: 'k2'l = [1]ll = iter(l)print(next(ll))print(next(ll)) #StopIteration
x #NameError: name 'x' is not definedint('abc') #ValueError: invalid literal for int() with base 10: 'abc'l = []l[10000] #IndexError: list index out of rangeclass Foo:passFoo.x #AttributeError: type object 'Foo' has no attribute 'x'1/0 #ZeroDivisionError: division by zerodic = {'k':'v'}dic['k2'] #KeyError: 'k2'l = [1]ll = iter(l)print(next(ll))print(next(ll)) #StopIteration
ArithmeticErrorAssertionErrorAttributeErrorBaseExceptionBufferErrorBytesWarningDeprecationWarningEnvironmentErrorEOFErrorExceptionFloatingPointErrorFutureWarningGeneratorExitImportErrorImportWarningIndentationErrorIndexErrorIOErrorKeyboardInterruptKeyErrorLookupErrorMemoryErrorNameErrorNotImplementedErrorOSErrorOverflowErrorPendingDeprecationWarningReferenceErrorRuntimeErrorRuntimeWarningStandardErrorStopIterationSyntaxErrorSyntaxWarningSystemErrorSystemExitTabErrorTypeErrorUnboundLocalErrorUnicodeDecodeErrorUnicodeEncodeErrorUnicodeErrorUnicodeTranslateErrorUnicodeWarningUserWarningValueErrorWarningZeroDivisionError
2、异常处理
1)何为异常
异常发生之后
异常之后的代码就不执行了也就是我们所说的bug
2)何为异常处理
python解释器检测到错误,触发异常(也允许程序员自己抛出异常)
程序员编写特定的代码,专门用来捕捉这个异常(这段代码与程序逻辑无关,与异常处理有关)
如果捕捉成功则进入另外一个处理分支,执行你为其定制的逻辑,使程序不会崩溃,这就是异常处理
3)为什么要进行异常处理
python解析器去执行程序,检测到了一个错误时,触发异常,异常触发后且没被处理的情况下,程序就在当前异常处终止,后面的代码不会运行,谁会去用一个运行着突然就崩溃的软件。
所以你必须提供一种异常处理机制来增强你程序的健壮性与容错性
3、异常处理
1)基本处理
try: 被检测的代码块except 异常类型: try中一旦检测到异常,就执行这个位置的逻辑
try: int(num)except ValueError: print('请输入一个数字')
异常类只能用来处理指定的异常情况,如果非指定异常则无法处理。
# 未捕获到异常,程序直接报错 s1 = 'hello'try: int(s1)except IndexError as e: print e
2)多分支
s1 = 'hello'try: int(s1)except IndexError as e: print(e)except KeyError as e: print(e)except ValueError as e: print(e)
3)万能异常
万能异常 在python的异常中,有一个万能异常:Exception,他可以捕获任意异常
s1 = 'hello'try: int(s1)except Exception as e: print(e)
不推荐使用因为捕捉到异常后不好查找,所有异常用同样方式处理也不友好
4)异常的其他机构
s1 = 'hello'try: int(s1)except IndexError as e: print(e)except KeyError as e: print(e)except ValueError as e: print(e)#except Exception as e:# print(e)else: print('try内代码块没有异常则执行我')finally: print('无论异常与否,都会执行该模块,通常是进行清理工作')
5)主动抛异常
try: raise TypeError('类型错误')except Exception as e: print(e)
6)自定义异常
class EvaException(BaseException): def __init__(self,msg): self.msg=msgtry: raise EvaException('类型错误')except EvaException as e: print(e)
7)断言
# assert 条件 assert 1 == 1 assert 1 == 2 #一个会抛出异常的判断#基本很难会用到,只有一些特定场合
4、什么时候用异常处理
当在这一块代码块这里可能出现的问题千奇百怪,去解决你想不到的具体的错误的时候
你能想到有异常,并且可能出现在这一块代码的异常有很多种,不能一一枚举
禁止在大段代码外面套异常处理
如果仅仅是预防以及在你能想到的情况范围内做限制可以使用if
- python进阶(8):常用模块2+异常处理
- Python进阶08 异常处理
- Python进阶08 异常处理
- Python进阶08 异常处理
- Python进阶08 异常处理
- python进阶教程之异常处理
- 8.Python进阶_异常处理
- python进阶——异常处理
- Java进阶(异常处理)
- python基础2 (函数,模块,文件I/O,异常处理)
- python urllib2 模块异常处理方法
- python的Socketserver模块使用,异常处理
- python基础-random、os模块、异常处理模块
- python常用的时间处理模块time
- python---异常处理2
- (Python)异常处理
- Python基础(7)- python各类模块、正则表达式、元字符、异常处理
- PHP进阶(第六弹:异常处理)
- 音乐播放器(1)
- 使用TCP协议和多线程实现实时聊天室
- 【暑期多校联萌
- 作用域与闭包
- PowerDesigner的报表功能导出表的清单
- python进阶(8):常用模块2+异常处理
- 生成树个数(基尔霍夫矩阵)
- [编程题] 彩色的砖块
- 17、坐标移动(string转char[])
- Stacking Plates UVA
- 用telnet测试给163邮箱发邮件
- 使用intent在活动之间穿梭——使用显式Intent
- ROBOCON——TITR——2015回顾
- 关于Handler进行倒计时服务