模块介绍
本节简介:
1.1 时间模块
1.2 random模块
1.3 shutil模块
1.4 shelve模块
1.5 XML模块
1.6 ConfigParser模块
1.7 hashlib模块
1.8 logging模块
1.9 re模块
1.20 subprocess模块
1.1 时间模块
time
print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.altzone) #返回与utc时间的时间差,以秒计算\
print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016",
print(time.localtime()) #返回本地时间 的struct time对象格式
print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
日期字符串 转成 时间戳
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_2_struct)
struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
print(struct_2_stamp)
将时间戳转为字符串格式
print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
时间加减
import datetime
print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
1.2 random模块
>>> import random
>>> print(random.random())
0.208189160969
>>> print(random.randint(1,5)) #随机打印1-5数据
5
>>> print(random.randrange(1,5)) #随机打印1-4
1
>>> import string
>>> str_source = string.ascii_letters + string.digits
>>> print(random.sample(str_source,7)) #随机打印str_source中的7个字符
['s', 'E', 'w', 'Z', 'h', 'I', 'N']
生成随机验证码>>:
import random
checkcode = ''
for i in range(4): #四位随机码
current = random.randrange(0,4) #思维随机码
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
print checkcode
1.3 shutil模块
shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中,可以部分内容
shutil.copyfile(src, dst)
拷贝文件
shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变
shutil.copystat(src, dst)
拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copy(src, dst)
拷贝文件和权限
shutil.copy2(src, dst)
拷贝文件和状态信息
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件
shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
shutil.move(src, dst)
递归的去移动文件
shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
- base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/ - format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要压缩的文件夹路径(默认当前目录)
- owner: 用户,默认当前用户
- group: 组,默认当前组
- logger: 用于记录日志,通常是logging.Logger对象
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
import zipfile # 压缩 z = zipfile.ZipFile('laxi.zip', 'w') z.write('a.log') z.write('data.data') z.close() # 解压 z = zipfile.ZipFile('laxi.zip', 'r') z.extractall() z.close()
--------------------------------------
# 压缩 tar = tarfile.open('your.tar','w') tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip') tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip') tar.close() # 解压 tar = tarfile.open('your.tar','r') tar.extractall() # 可设置解压地址 tar.close()
-------------------------------------
# shutil.copyfile("time-datetime.py","timeeme") #拷贝文件
# shutil.copy2("time-datetime.py","time_bak.txt") #拷贝文件和状态信息
# shutil.copytree(r"D:\python培训\our_python\day3",'day3_bak') #递归的去拷贝文件
# shutil.rmtree('day3_bak') #递归的去删除文件
# shutil.move('day3_bak','day3_bak2') #移动文件
1.4 shelve模块
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
保存数据:
import shelve
d = shelve.open('shelve_test')
#存函数
def stu_date(name,age):
print("std",name,age)
#列表
name = ['zs','ls','we']
#字典
data = {'name':'zs','age':'22'}
#存储
d['func'] = stu_date
d['lis'] = name
d['dic'] = data
读数据:
import shelve
def stu_date(name,age):
print("std",name,age)
f = shelve.open('shelve_test')
print(f['func']('zhangsong',23))
print(f['lis'])
print(f['dic'])
1.5 XML模块
创建XML文件:
import xml.etree.ElementTree as ET
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked": "no"})
sex = ET.SubElement(name, "sex")
sex.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19'
et = ET.ElementTree(new_xml) # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)
ET.dump(new_xml) # 打印生成的格式
删除XML文件:
import xml.etree.ElementTree as ET
tree = ET.parse("xml_test.xml")
root = tree.getroot()
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')
修改XML文件:
import xml.etree.ElementTree as ET
tree = ET.parse("xml_test_bak.xml")
root = tree.getroot()
# 修改 先将文件全部读入内存,然后采用循环逐条取出数据,修改数据后将数据另存为其他文件或者存回原文件
for node in root.iter('year'):
# print(node,type(node))
# print(node,type(node.text))
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated", "yes")
tree.write("xml_test_bak.xml")
读XML文件:
import xml.etree.ElementTree as ET
tree = ET.parse("xml_test.xml")
root = tree.getroot()
print(root.tag)
# 遍历xml文档
# for child in root:
# print(child.tag, child.attrib)
# for i in child:
# print('\t',i.tag, i.text)
#
# 遍历xml文档中的year
# for child in root:
# #print(child.tag, child.attrib)
# for i in child.iter('year'):
# print('\t',i.tag, i.text)
# # 只遍历year 节点
# for node in root.iter('year'):
# print(node.tag, node.text)
1.6 ConfigParser模块
创建文件:
import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('config.ini', 'w') as configfile:
config.write(configfile)
读文件:
import configparser
config = configparser.ConfigParser()
# print(config.sections())
config.read('config.ini')
print(config.sections())
# print('bitbucket.org' in config)
print(config['bitbucket.org']['User'])
print(config['DEFAULT']['Compression'])
topsecret = config['topsecret.server.com']
print(topsecret['host port'])
print("循环".center(20,'-'))
for k in config['bitbucket.org']: #读取指定域和DEFAULT域的key
print(k)
文件操作:
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
# ########## 读 ##########
# secs = config.sections()
# print secs
# options = config.options('group2')
# print options
# item_list = config.items('group2')
# print item_list
# val = config.get('group1','key')
# val = config.getint('group1','key')
# ########## 改写 ##########
# sec = config.remove_section('group1')
# config.write(open('i.cfg', "w"))
# sec = config.has_section('wupeiqi')
# sec = config.add_section('wupeiqi')
# config.write(open('i.cfg', "w"))
# config.set('group2','k1',11111)
# config.write(open('i.cfg', "w"))
# config.remove_option('group2','age')
# config.write(open('i.cfg', "w"))
1.7 hashlib模块
import hashlib
m = hashlib.md5()
m.update(b"Hello")
m.update(b"It's me")
#print(m.digest())
m.update(b"It's been a long time since last time we ...")
#print(m.digest()) # 2进制格式hash
#print(len(m.hexdigest())) # 16进制格式hash
'''
def digest(self, *args, **kwargs): # real signature unknown
""" Return the digest value as a string of binary data. """
pass
def hexdigest(self, *args, **kwargs): # real signature unknown
""" Return the digest value as a string of hexadecimal digits. """
pass
'''
import hashlib
# ######## md5 ########
hash = hashlib.md5()
hash.update(b'admin')
print("md5")
print(hash.hexdigest())
# ######## sha1 ########
hash = hashlib.sha1()
hash.update(b'admin')
print("sh1")
print(hash.hexdigest())
# ######## sha256 ########
hash = hashlib.sha256()
hash.update(b'admin')
print("sh256")
print(hash.hexdigest())
# ######## sha384 ########
hash = hashlib.sha384()
hash.update(b'admin')
print("sh384")
print(hash.hexdigest())
# ######## sha512 ########
hash = hashlib.sha512()
hash.update(b'admin')
print("sh512")
print(hash.hexdigest())
1.8 logging模块
指定日志级别:
import logging
#日志输出到文件并指定日志级别
#logging.basicConfig(filename='test.log',level=logging.INFO)
#为日志加上时间
logging.basicConfig(format='%(asctime)s %(filename)s-%(lineno)d %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.debug("user [alex] attempted wrong password more than 1 times")
logging.info("user [alex] attempted wrong password more than 2 times")
logging.warning("user [alex] attempted wrong password more than 3 times")
logging.critical("server is down")
不同级别的日志输出:
import logging
# create logger
logger = logging.getLogger('TEST-LOG')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create file handler and set level to warning
fh = logging.FileHandler("access.log")
fh.setLevel(logging.ERROR)
# create formatter
ch_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch and fh
ch.setFormatter(ch_formatter)
fh.setFormatter(fh_formatter)
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
1.20 subprocess模块
参数shell=True,默认False,shell = True,允许 shell 命令是字符串形式
#执行命令,返回命令执行状态 , 0 or 非0
>>> retcode = subprocess.call(["ls", "-l"])
#执行命令,如果命令结果为0,就正常返回,否则抛异常
>>> subprocess.check_call(["ls", "-l"])
0
#接收字符串格式命令,返回元组形式,第1个元素是执行状态,第2个是命令结果
>>> subprocess.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
#接收字符串格式命令,并返回结果
>>> subprocess.getoutput('ls /bin/ls')
'/bin/ls'
#执行命令,并返回结果,注意是返回结果,不是打印,下例结果返回给res
>>> res=subprocess.check_output(['ls','-l'])
>>> res
b'total 0\ndrwxr-xr-x 12 alex staff 408 Nov 2 11:05 OldBoyCRM\n'
===========
#上面那些方法,底层都是封装的subprocess.Popen
调用subprocess.run(...)是推荐的常用方法,在大多数情况下能满足需求,但如果你可能需要进行一些复杂的与系统的交互的话,你还可以用subprocess.Popen(),语法如下:
p = subprocess.Popen("find / -size +1000000 -exec ls -shl {} \;",shell=True,stdout=subprocess.PIPE)
print(p.stdout.read())
相关参数:
args:shell命令,可以是字符串或者序列类型(如:list,元组)
bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
shell:参数shell=True,默认False,shell = True,允许 shell 命令是字符串形式
cwd:用于设置子进程的当前目录
env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
startupinfo与createionflags只在windows下有效
将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
poll()
Check if child process has terminated. Returns returncode
wait()
Wait for child process to terminate. Returns returncode attribute.
terminate() 杀掉所启动进程
communicate() 等待任务结束
stdin 标准输入
stdout 标准输出
stderr 标准错误
pid
The process ID of the child process.
#例子
>>> p = subprocess.Popen("df -h|grep disk",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
>>> p.stdout.read()
b'/dev/disk1 465Gi 64Gi 400Gi 14% 16901472 104938142 14% /\n'
>>> p = subprocess.Popen("df -h",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
>>> print p.stdout.read()
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 48G 6.6G 41G 14% /
devtmpfs 482M 0 482M 0% /dev
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 492M 50M 443M 11% /run
tmpfs 492M 0 492M 0% /sys/fs/cgroup
/dev/xvda1 497M 129M 369M 26% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/loop0 4.1G 4.1G 0 100% /mnt
>>> p = subprocess.Popen("sleep 10;df -h|grep /dev/sr0",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> print(p.poll())
None
>>> print(p.poll())
0
>>> p = subprocess.Popen("sleep 10;df -h|grep /dev/sr0",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> p.wait()
0
>>> subprocess.run(["ls", "-l"]) # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')
终端输入的命令分为两种:
A:输入即可得到输出,如:ifconfig(查看以上实例)
B:输入进行某环境,依赖再输入,如:python(详见下方实例)
需要交互的命令示例
import subprocess
obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write('print 1 \n ')
obj.stdin.write('print 2 \n ')
obj.stdin.write('print 3 \n ')
obj.stdin.write('print 4 \n ')
out_error_list = obj.communicate(timeout=10)
print out_error_list
subprocess实现sudo 自动输入密码
import subprocess
def mypass():
mypass = '123' #or get the password from anywhere
return mypass
echo = subprocess.Popen(['echo',mypass()],
stdout=subprocess.PIPE,
)
sudo = subprocess.Popen(['sudo','-S','iptables','-L'],
stdin=echo.stdout,
stdout=subprocess.PIPE,
)
end_of_pipe = sudo.stdout
print "Password ok \n Iptables Chains %s" % end_of_pipe.read()
其他说明: https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments