【python】【内置模块】【二】【sys subprocess hashlib】

3. sys

  这个模块可供访问由解释器使用或维护的变量和与解释器进行交互的函数

变量/函数 含义 样例
argv 向程序传递的参数
test.py
#!/usr/bin/env python
import sys
print sys.argv
>>> python test.py 1 2 ['test.py', '1', '2']
path 获取指定模块搜索路径的字符串集合
>>> sys.path
['/Library/Python/2.7/site-packages', *****]
platform 获取当前熊平台
>>> print sys.platform
darwin
stdin stdout stderr 标准输入、输出、错误流  
exit()  退出程序
>>> sys.exit(0)

Process finished with exit code 0
getdefaultencoding() 获取系统当前编码
>>> print sys.getdefaultencoding()
ascii
setdefaultencoding() 设置系统当前编码  
>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf-8')
>>> sys.getdefaultencoding()
'utf-8'
getfilesystemencoding() 获取文件系统编码
>>> sys.getfilesystemencoding()
'utf-8'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4. subprocess

  subprocess模块是python从2.4版本开始引入的模块。主要用来取代 一些旧的模块方法,如os.system、os.spawn*、os.popen*、commands.*等。subprocess通过子进程来执行外部指令,并通过input/output/error管道,获取子进程的执行的返回信息。

变量/函数 含义 样例
PIPE -1  
STDOUT -2  
Popen 类似os.popen*() 
import subprocess
p = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) cout, cerr = p.communicate('print "this is test."') print cout p = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.stdin.write('print "this is test."') p.stdin.close() print p.stdout.read() p.stdout.close()
call() 执行命令,类似os.system
>>> subprocess.call('echo this is test.', shell=True)
this is test.
0
>>> subprocess.call(['echo', 'this is test.'], shell=False)
this is test.
0
check_call()  用法与subprocess.call()类似,区别是,当返回值不为0时,直接抛出异常
>>> subprocess.call('fdkfjk', shell=True)
/bin/sh: fdkfjk: command not found
127
>>> subprocess.check_call('fdkfjk', shell=True)
/bin/sh: fdkfjk: command not found
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "******/subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
CalledProcessError: Command 'fdkfjk' returned non-zero exit status 127
check_output() 同check_call,不过返回输出结构
>>> subprocess.check_output('echo this is test.', shell=True)
'this is test.\n'

 

 5. hashlib

变量/函数 含义 样例
algorithms 一个列明该模块所支持的算法名称的元组,Python 2.7 引入
>>> hashlib.algorithms
('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
algorithms_guaranteed 一个包含该模块跨所有平台都支持的算法名称的集合,Python 2.7.9 引入
>>> hashlib.algorithms_guaranteed
set(['sha1', 'sha224', 'sha384', 'sha256', 'sha512', 'md5'])
algorithms_available  它的值是是一个当前运行的Python解释器中可用的哈希算法的名称集合,Python2.7.9引入  
>>> hashlib.algorithms_available
set(['SHA1', 'MDC2', 'SHA224', 'SHA', 'SHA384',
'ecdsa-with-SHA1', 'SHA256', 'SHA512', 'md4',
'md5', 'sha1', 'dsaWithSHA', 'DSA-SHA', 'sha',
'sha224', 'dsaEncryption', 'DSA', 'RIPEMD160',
'mdc2', 'MD5', 'MD4', 'sha384', 'sha256', 'sha512', 'ripemd160'])
md5() md5摘要算法
>>> md5 = hashlib.md5()
>>> md5.update('this is text.')
>>> md5.hexdigest()
'aa3f8b068561cef0f9ccc69d6c0342ab'
>>> md5.digest()
'\xaa?\x8b\x06\x85a\xce\xf0\xf9\xcc\xc6\x9dl\x03B\xab'
>>> md5 = hashlib.md5()
>>> md5.update('this is ')
>>> md5.update('text.')
>>> md5.hexdigest()
'aa3f8b068561cef0f9ccc69d6c0342ab'

sha1()
sha224()
sha256()
sha384()
sha512()

sha摘要算法 用法同md5()
new() 通过算法名获取一个hash对象
>>> md5 = hashlib.new('md5', 'this is text.')
>>> md5.hexdigest()
'aa3f8b068561cef0f9ccc69d6c0342ab'

 

posted @ 2018-03-07 08:14  aclove  阅读(85)  评论(0)    收藏  举报