Python全栈开发——subprocess & struct

import subprocess
def cmd_res(cmd):
    cmd_res=subprocess.Popen(cmd,shell=True,
                     stderr=subprocess.PIPE,
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE
                     )

    err=cmd_res.stderr.read()
#结果的编码是以当前所在的系统为准的,
# 如果是windows,那么res.stderr.read()读出的就是GBK编码的,
# 在接收端需要用GBK解if err:
        cmd_date=err
    else:
        cmd_date=cmd_res.stdout.read()
    return cmd_date

if __name__=='__main__':
    cmd=input('>>>')
    res=cmd_res(cmd)
    print(res)   # 二进制文件
    print(res.decode('gbk')) #解码
import struct

date=123
string=struct.pack('i',date)
print(string)  #b'{x00x00x00'
t=struct.unpack('i',string)
print(t)
原文地址:https://www.cnblogs.com/lujiacheng-Python/p/9781371.html