Python中sys,hashlib模块

Python sys模块

sys模块功能多,全记下来完全没必要,下面说一些比较实用的;

1、sys.argv

在外部向程序内部传递参数,获取运行 Python 程序的命令行参数。其中 sys.argv[0] 通常就是指该 Python 程序,sys.argv[1] 代表为 Python 程序提供的第一个参数,sys.argv[2] 代表为 Python 程序提供的第二个参数……依此类推;

#!/usr/bin/python

import sys
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2])

[root@server-7 py]# ./sys.py test hello
./sys.py
test
hello
[root@server-7 py]# ./sys.py test hello 123
./sys.py
test
hello

2、sys.path

该属性指定 Python 查找模块的路径列表。程序可通过修改该属性来动态增加 Python 加载模块的路径;

>>> import sys
>>> sys.path
['', '/usr/local/python3/lib/python36.zip', '/usr/local/python3/lib/python3.6', '/usr/local/python3/lib/python3.6/lib-dynload', '/usr/local/python3/lib/python3.6/site-packages']

3、sys.modules

返回模块名和载入模块对应关系的字典;

>>> import sys
>>> print(sys.modules.keys())
dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp', '_warnings', '_thread', '_weakref', '_frozen_importlib_external', '_io', 'marshal', 'posix', 'zipimport', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_weakrefset', 'site', 'os', 'errno', 'stat', '_stat', 'posixpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', 'sysconfig', '_sysconfigdata_m_linux_x86_64-linux-gnu', '_bootlocale', '_locale', 'types', 'functools', '_functools', 'collections', 'operator', '_operator', 'keyword', 'heapq', '_heapq', 'itertools', 'reprlib', '_collections', 'weakref', 'collections.abc', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'warnings', 'importlib.util', 'importlib.abc', 'importlib.machinery', 'contextlib', 'zope', 'readline', 'atexit', 'rlcompleter'])
>>> print(sys.modules.values())
dict_values([<module 'builtins' (built-in)>, <module 'sys' (built-in)>, <module 'importlib._bootstrap' (frozen)>, <module '_imp' (built-in)>, <module '_warnings' (built-in)>, <module '_thread' (built-in)>, <module '_weakref' (built-in)>, <module 'importlib._bootstrap_external' (frozen)>, <module 'io' (built-in)>, <module 'marshal' (built-in)>, <module 'posix' (built-in)>, <module 'zipimport' (built-in)>, <module 'encodings' from '/usr/local/python3/lib/python3.6/encodings/__init__.py'>, <module 'codecs' from '/usr/local/python3/lib/python3.6/codecs.py'>, <module '_codecs' (built-in)>, <module 'encodings.aliases' from '/usr/local/python3/lib/python3.6/encodings/aliases.py'>, <module 'encodings.utf_8' from '/usr/local/python3/lib/python3.6/encodings/utf_8.py'>, <module '_signal' (built-in)>, <module '__main__' (built-in)>, <module 'encodings.latin_1' from '/usr/local/python3/lib/python3.6/encodings/latin_1.py'>, <module 'io' from '/usr/local/python3/lib/python3.6/io.py'>, <module 'abc' from '/usr/local/python3/lib/python3.6/abc.py'>, <module '_weakrefset' from '/usr/local/python3/lib/python3.6/_weakrefset.py'>, <module 'site' from '/usr/local/python3/lib/python3.6/site.py'>, <module 'os' from '/usr/local/python3/lib/python3.6/os.py'>, <module 'errno' (built-in)>, <module 'stat' from '/usr/local/python3/lib/python3.6/stat.py'>, <module '_stat' (built-in)>, <module 'posixpath' from '/usr/local/python3/lib/python3.6/posixpath.py'>, <module 'genericpath' from '/usr/local/python3/lib/python3.6/genericpath.py'>, <module 'posixpath' from '/usr/local/python3/lib/python3.6/posixpath.py'>, <module '_collections_abc' from '/usr/local/python3/lib/python3.6/_collections_abc.py'>, <module '_sitebuiltins' from '/usr/local/python3/lib/python3.6/_sitebuiltins.py'>, <module 'sysconfig' from '/usr/local/python3/lib/python3.6/sysconfig.py'>, <module '_sysconfigdata_m_linux_x86_64-linux-gnu' from '/usr/local/python3/lib/python3.6/_sysconfigdata_m_linux_x86_64-linux-gnu.py'>, <module '_bootlocale' from '/usr/local/python3/lib/python3.6/_bootlocale.py'>, <module '_locale' (built-in)>, <module 'types' from '/usr/local/python3/lib/python3.6/types.py'>, <module 'functools' from '/usr/local/python3/lib/python3.6/functools.py'>, <module '_functools' (built-in)>, <module 'collections' from '/usr/local/python3/lib/python3.6/collections/__init__.py'>, <module 'operator' from '/usr/local/python3/lib/python3.6/operator.py'>, <module '_operator' (built-in)>, <module 'keyword' from '/usr/local/python3/lib/python3.6/keyword.py'>, <module 'heapq' from '/usr/local/python3/lib/python3.6/heapq.py'>, <module '_heapq' from '/usr/local/python3/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so'>, <module 'itertools' (built-in)>, <module 'reprlib' from '/usr/local/python3/lib/python3.6/reprlib.py'>, <module '_collections' (built-in)>, <module 'weakref' from '/usr/local/python3/lib/python3.6/weakref.py'>, <module 'collections.abc' from '/usr/local/python3/lib/python3.6/collections/abc.py'>, <module 'importlib' from '/usr/local/python3/lib/python3.6/importlib/__init__.py'>, <module 'importlib._bootstrap' (frozen)>, <module 'importlib._bootstrap_external' (frozen)>, <module 'warnings' from '/usr/local/python3/lib/python3.6/warnings.py'>, <module 'importlib.util' from '/usr/local/python3/lib/python3.6/importlib/util.py'>, <module 'importlib.abc' from '/usr/local/python3/lib/python3.6/importlib/abc.py'>, <module 'importlib.machinery' from '/usr/local/python3/lib/python3.6/importlib/machinery.py'>, <module 'contextlib' from '/usr/local/python3/lib/python3.6/contextlib.py'>, <module 'zope' (namespace)>, <module 'readline' from '/usr/local/python3/lib/python3.6/lib-dynload/readline.cpython-36m-x86_64-linux-gnu.so'>, <module 'atexit' (built-in)>, <module 'rlcompleter' from '/usr/local/python3/lib/python3.6/rlcompleter.py'>])
>>> print(sys.modules["os"])
<module 'os' from '/usr/local/python3/lib/python3.6/os.py'>

Python hashlib模块

用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法,

以常见的MD5为例,计算出一个字符串的MD5值:

>>> import hashlib
>>> m = hashlib.md5()
>>> print(m)
<md5 HASH object @ 0x7fbb1d51b5a8>
>>> m = hashlib.md5('hello world 123'.encode('utf8'))
>>> print(m.hexdigest())
7f797e9a4e2c3a9b190225d299214ce4

如果数据量很大,可以分块多次调用update(),最后计算的结果是一样的:

>>> import hashlib
>>> m = hashlib.md5('helloworld123'.encode('utf8'))
>>> print(m.hexdigest())
cd1b8ecf103743a98958211a11e33b71
>>> m = hashlib.md5()
>>> m.update('hello'.encode('utf8'))
>>> m.update('world'.encode('utf8'))
>>> m.update('123'.encode('utf8'))
>>> print(m.hexdigest())
cd1b8ecf103743a98958211a11e33b71

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值