Python —— 模块补充

#subprocess

stdout: 标准输出

stdin: 标准输入

stderr: 标准错误输出

subprocess是os.system的升级版,可以在python中执行shell命令,并且可以通过管道获取stdout、stdin、stderr

 1 import subprocess
 2 #这样相当于执行了ls,执行的结果直接给了屏幕
 3 #res = subprocess.Popen('ls', shell=True)
 4 #我们可以用管道把结果给接收,这里得到的res相当于那个接受了信息的管道(是一个对象)
 5 
 6 #将这个管道里的信息取出(字节形式),但注意取过一次后就空了
 7 #stdout、stdin、stderr的信息分别存于不同的管道
 8 res = subprocess.Popen('ls', shell=True,
 9                                    stdout=subprocess.PIPE,
10                                    stdin=subprocess.PIPE,
11                                    stderr=subprocess.PIPE)
12 print(res.stdout.read().decode('utf8'))
13 
14 res = subprocess.Popen('错误命令', shell=True,
15                                    stdout=subprocess.PIPE,
16                                    stdin=subprocess.PIPE,
17                                    stderr=subprocess.PIPE)
18 print(res.stderr.read().decode('utf8'))

 #communicate

Popen.communicate(input=None, timeout=None) 该方法可用来与进程进行交互,比如发送数据到stdin,从stdout和stderr读取数据,直到到达文件末尾。

关于communicate()方法的说明:

  • 该方法中的可选参数 input 应该是将被发送给子进程的数据,或者如没有数据发送给子进程,该参数应该是None。input参数的数据类型必须是字节串,如果universal_newlines参数值为True,则input参数的数据类型必须是字符串。
  • 该方法返回一个元组(stdout_data, stderr_data),这些数据将会是字节穿或字符串(如果universal_newlines的值为True)。
  • 如果在timeout指定的秒数后该进程还没有结束,将会抛出一个TimeoutExpired异常。捕获这个异常,然后重新尝试通信不会丢失任何输出的数据。但是超时之后子进程并没有被杀死,为了合理的清除相应的内容,一个好的应用应该手动杀死这个子进程来结束通信。
  • 需要注意的是,这里读取的数据是缓冲在内存中的,所以,如果数据大小非常大或者是无限的,就不应该使用这个方法

 以下几个实例可以帮助理解如何使用communicate:

1. 标准化输入

 1 import subprocess
 2 
 3 #两种方法进行标准化输入
 4 #方法一
 5 res = subprocess.Popen('python', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
 6 res.stdin.write('print(1)\n'.encode('utf8'))
 7 res.stdin.write('print(2)\n'.encode('utf8'))
 8 res.stdin.write('print(3)\n'.encode('utf8'))
 9 out, err = res.communicate()
10 print(out, err, sep='\n')
11 
12 print('-----------------------')
13 #方法二
14 res = subprocess.Popen('python', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
15 out, err = res.communicate(input="print('hello')".encode('utf8'))
16 print(out, err, sep='\n')

 2. 实现类似于shell中管道的功能

1 #相当于: ls -lh | grep py
2 p1 = subprocess.Popen('ls -lh', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
3 p2 = subprocess.Popen("grep py ", shell=True, stdout=subprocess.PIPE, stdin=p1.stdout, stderr=subprocess.PIPE)
4 out, err = p2.communicate()
5 print(out.decode('utf8'), err)

 参考链接:https://www.cnblogs.com/yyds/p/7288916.html

#struct

该模块可以把一个类型,如数字,转成固定长度的bytes

1 import struct
2 a = 10000
3 b = str(a).encode('utf8')
4 c = struct.pack('i', a)         #i代表需要被转换的a的类型
5 print(b, len(b))
6 print(c, len(c))
7 d = struct.unpack('i', c)       #反解
8 print(d)

#partial

函数式编程中的一个模块,可以固定住函数传入的第一个参数

1 from functools import partial
2 
3 def add(x, y):
4      return x + y
5  
6 func = partial(add, 1)      #偏函数
7 print(func(1))  

 

#optparse

用于获取python文件的输入参数,是sys.argv的升级

import optparse

op = optparse.OptionParser()
#指定输入的参数
op.add_option('-s', '--server', dest='server')
op.add_option('-p', '--P', dest='port')

#参数解析
options, argv = op.parse_args()
#指定的输入参数值
print(options)
#未指定的输入参数值
print(argv)
#注意option不是一个字典,是一个optparse.Values 类
print(type(options))
#不能用字典那样取,而是操作这个类的属性来取
print(options.server)
print(options.port)

 

 

 

 

 

 

                  

posted @ 2019-12-08 17:28  Matrixssy  阅读(257)  评论(0编辑  收藏  举报