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 Files\JetBrains\s7\day28\log.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' \xc7\xfd\xb6\xaf\xc6\xf7 D \xd6\xd0\xb5\xc4\xbe\xed\xca\xc7 DATA\r\n \xbe\xed\xb5\xc4\xd0\xf2\xc1\xd0\xba\xc5\xca\xc7 D64A-0BF1\r\n\r\n D:\\Program Files\\JetBrains\\s7\\day30 \xb5\xc4\xc4\xbf\xc2\xbc\r\n\r\n2017/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 Files\JetBrains\s7\day32 ��Ŀ¼ 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 �����ֽ�
python hashlib模块 logging模块 subprocess模块的更多相关文章
- python常用模块补充hashlib configparser logging,subprocess模块
一.hashlib模板 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...
- python基础语法13 内置模块 subprocess,re模块,logging日志记录模块,防止导入模块时自动执行测试功能,包的理论
subprocess模块: - 可以通过python代码给操作系统终端发送命令, 并且可以返回结果. sub: 子 process: 进程 import subprocess while Tru ...
- python笔记8 socket(TCP) subprocess模块 粘包现象 struct模块 基于UDP的套接字协议
socket 基于tcp协议socket 服务端 import socket phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 买 ...
- random模块、os模块、序列化模块、sy模块s、subprocess模块
random随机数模块 random.random( ) 随机产生一个0-1之间的小数 print(random.random()) # 0.31595547439342897 random.rand ...
- Python模块之OS,subprocess
1.os 模块 简述: os 表示操作系统 该模块主要用来处理与系统相关操作 最常用的是文件操作 打开 获取 写入 删除 复制 重命名 常用操作 os.getcwd() : 返回当前文件所在文件夹路径 ...
- subprocess模块(了解)
目录 一.subprocess模块 一.subprocess模块 subprocess模块允许你去创建一个新的进程让其执行另外的程序,并与它进行通信,获取标准的输入.标准输出.标准错误以及返回码等.更 ...
- 网络编程 --- subprocess模块,struct模块,粘包,UDP协议,socket_server模块
目录 subprocess模块 struct模块 粘包 UDP协议 socket_server模块 subprocess模块 作用: 1.可以帮你通过代码执行操作系统的终端命令 2.并返回终端执行命令 ...
- Python自动化运维之9、模块之sys、os、hashlib、random、time&datetime、logging、subprocess
python模块 用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- Python基础之模块:6、hashlib模块 subprocess模块 logging模块
目录 一.hashlib模块 1.简介 2.基本操作与用法 二.subprocess模块 1.简介 2.基本操作与用法 三.logging模块 1.简介 2.基本操作与用法 一.hashlib模块 1 ...
随机推荐
- Python socket 粘包
目录 1 TCP的三次握手四次挥手 0 1.1 三次握手 1 1.2 四次挥手 2 2 粘包现象 3 2.1 基于TCP制作远程执行命令操作(win服务端) 4 2.1 基于TCP制作远程执行命令操作 ...
- python之道13
看代码分析结果 func_list = [] for i in range(10): func_list.append(lambda :i) v1 = func_list[0]() v2 = func ...
- 面向对象OO第一单元三次作业总结
(一)第一单元的作业围绕着多项式的求导,从简单到复杂,主要的要求是 作业一:只有两种格式的因子:带符号整数(+02)和幂函数(x^+02). 作业二:在作业一的基础上添加了:sin(x)和cos(x) ...
- java基础—多态(动态加载)
一.面向对象最核心的机制——动态绑定,也叫多态
- 使用lua实现Spine动画的预加载
创建spine动画有两种方法,分别是createwithfile和createwithdata. createWithFile是通过加载动作数据马上进行创建,如果spine动画中的json文件大小超过 ...
- 【点分治】luoguP2664 树上游戏
应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...
- linux关于yum
yum仓库设置:1.cd /etc/yum.repos.d yum仓库 2.CentOS-Base.repo 网络源 CentOS-Media.repo 光盘源 设置 vi CentOS-Media. ...
- 有关Kali的方法
Kali 找回系统登陆密码的方式:https://jingyan.baidu.com/article/47a29f24560e77c0142399e3.html
- LeetCode(172)Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- hdu 3836 tarjain 求强连通分量个数
// 给你一个有向图,问你最少加几条边能使得该图强连通 #include <iostream> #include <cstdio> #include <cstring&g ...