python hashlib模块 logging模块 subprocess模块

一 hashlib模块

import hashlib
md5=hashlib.md5()        #可以传参,加盐处理
print(md5)
md5.update(b'alex')      #update参数必须是byte类型
md5.update(b'sb')
print(md5.hexdigest())    #结果与update(b'alexsb')是一样的

  输出:

<md5 HASH object @ 0x00000204693FC3C8>
3b30fab9b1de071c65055026862ce00e
import hashlib
md5=hashlib.md5()                                        #hashlib模块 md5类 加括号 实例化
print(md5)
# md5.update(b'alex')
# md5.update(b'sb')
md5.update(bytes('老男孩',encoding='utf-8'))
#bytes 将字符串格式转为byte格式 print(md5.hexdigest())

  输出:

6a7ece82e4ed94a475dab275891d5036

二 logging模块

import logging
logger=logging.getLogger()     #生成一个logger对象  <RootLogger root (WARNING)>
formatter=logging.Formatter()  #指定logger输出格式,最后要被logger调用。<logging.Formatter object at 0x00000198F07F2C88>
file_handler=logging.FileHandler('log.log')   #生成文件日志,<FileHandler D:Program FilesJetBrainss7day28log.log (NOTSET)>
console_handler=logging.StreamHandler()       #生成控制台日志,<StreamHandler <stderr> (NOTSET)>
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)       #通过setFormatter指定输出格式
logger.addHandler(file_handler)
logger.addHandler(console_handler)             #未logger对象添加日志处理器
logger.setLevel(logging.INFO)                  #指定日志的最低输出级别,默认是WARN级别
logger.debug('this is debug info')
logger.critical('thie is critical info')

  输出:

thie is critical info

三 subprocess模块

  subprocess:子流程

  Popen类:

  官方定义: Execute a child program in a new process.在新流程中执行子程序。

import subprocess            #subprocess模块
obj=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)   #obj是Poppe类实例化的一个对象,
print(obj,type(obj))                                                                   
res=obj.stdout.read()                                                                  #obj.stdout是  <_io.BufferedReader name=3>,调用read()方法,读取管道内的数据。
print(res)                                                                             #管道内的数据是二进制,在windows下是gbk编码的。

  输出:

<subprocess.Popen object at 0x000001873F349B00> <class 'subprocess.Popen'>
b' xc7xfdxb6xafxc6xf7 D xd6xd0xb5xc4xbexedxcaxc7 DATA
 xbexedxb5xc4xd0xf2xc1xd0xbaxc5xcaxc7 D64A-0BF1

 D:\Program Files\JetBrains\s7\day30 xb5xc4xc4xbfxc2xbc

2017/09/26 

  stdout,stdin的应用

import subprocess
obj1=subprocess.Popen('tasklist',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)             #tasklist,findstr都是可以在cmd下运行的指令
obj2=subprocess.Popen('findstr pycharm',shell=True,stdin=obj1.stdout,stdout=subprocess.PIPE)       #stdin=一个Popen对象的stdout
print(obj2.stdout.read().decode('gbk'))                                                                #windows下默认编码是gbk格式

  输出:

pycharm64.exe                 5564 Console                   11  1,012,500 K

==>

import subprocess
subprocess.Popen('dir',shell=True)            #调用Popen方法已经打开了一个子进程,执行了dir命令。没有std=subprocess.PIPE,看来就是默认输出屏幕了。

  输出:

 ������ D �еľ��� DATA
 �������� D64A-0BF1

 D:Program FilesJetBrainss7day32 ��Ŀ¼

2017/09/28  22:42    <DIR>          .
2017/09/28  22:42    <DIR>          ..
2017/09/28  22:42             1,520 c.py
2017/09/28  17:04               737 s.py
               2 ���ļ�          2,257 �ֽ�
               2 ��Ŀ¼ 374,639,685,632 �����ֽ�
原文地址:https://www.cnblogs.com/654321cc/p/7576969.html