美文网首页
python3 subprocess.check_output(

python3 subprocess.check_output(

作者: 违规昵称不予展示 | 来源:发表于2018-10-10 18:22 被阅读0次

大概的错误类型:

- AttributeError: 'str' object has no attribute 'decode'
 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc5 in position 13: invalid continuation byte

大概的输出应该长这样

- C:\Users\DELL>E:\App\py_work_3.7\Web\socket\netcat\mycat_sever.py
b'\r\nWindows IP \xc5\xe4\xd6\xc3\r\n\r\n\r\n\xd2\xd4\xcc\xab\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 VirtualBox Host-Only Network:\r\n\r\n   \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \r\n   \xb1\xbe\xb5\xd8\xc1\xb4\xbd\xd3 IPv6 \xb5\xd8\xd6\xb7. . . . . . . . : fe80::48cd:403c:b18c:b807%2\r\n   IPv4 \xb5\xd8\xd6\xb7 . . . . . . . . . . . . : 192.168.56.1\r\n   \xd7\xd3\xcd\xf8\xd1\xda\xc2\xeb  . . . . . . . . . . . . : 255.255.255.0\r\n   \xc4\xac\xc8\xcf\xcd\xf8\xb9\xd8. . . . . . . . . . . . . : \r\n\r\n\xce\xde\xcf\xdf\xbe\xd6\xd3\xf2\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 WLAN:\r\n\r\n   \xc3\xbd\xcc\xe5\xd7\xb4\xcc\xac  . . . . . . . . . . . . : \xc3\xbd\xcc\xe5\xd2\xd1\xb6\xcf\xbf\xaa\xc1\xac\xbd\xd3\r\n   \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \r\n\r\n\xce\xde\xcf\xdf\xbe\xd6\xd3\xf2\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 \xb1\xbe\xb5\xd8\xc1\xac\xbd\xd3* 1:\r\n\r\n   \xc3\xbd\xcc\xe5\xd7\xb4\xcc\xac  . . . . . . . . . . . . : \xc3\xbd\xcc\xe5\xd2\xd1\xb6\xcf\xbf\xaa\xc1\xac\xbd\xd3\r\n   \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \r\n\r\n\xce\xde\xcf\xdf\xbe\xd6\xd3\xf2\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 \xb1\xbe\xb5\xd8\xc1\xac\xbd\xd3* 11:\r\n\r\n   \xc3\xbd\xcc\xe5\xd7\xb4\xcc\xac  . . . . . . . . . . . . : \xc3\xbd\xcc\xe5\xd2\xd1\xb6\xcf\xbf\xaa\xc1\xac\xbd\xd3\r\n   \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \r\n\r\n\xd2\xd4\xcc\xab\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 \xd2\xd4\xcc\xab\xcd\xf8:\r\n\r\n   \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \r\n   \xb1\xbe\xb5\xd8\xc1\xb4\xbd\xd3 IPv6 \xb5\xd8\xd6\xb7. . . . . . . . : fe80::2c17:be54:7db6:f701%12\r\n   IPv4 \xb5\xd8\xd6\xb7 . . . . . . . . . . . . : 192.168.1.104\r\n   \xd7\xd3\xcd\xf8\xd1\xda\xc2\xeb  . . . . . . . . . . . . : 255.255.255.0\r\n   \xc4\xac\xc8\xcf\xcd\xf8\xb9\xd8. . . . . . . . . . . . . : 192.168.1.1\r\n'

中文全部变成了特定编码,而数字和英文不受影响

原理1

image.png

原理2

在windows中通过subprocess调用cmd命令行,命令中包含中文是很令人头痛的事。由于cmd控制台用的是gbk编码,而python用的是utf-8。utf-8的字符串,在gbk编码的控制台上运行,当然会运行不了。假如再要你兼容繁体版的windows,此时更麻烦了。还好python提供了本地化接口
本地化

>>> import locale
>>> locale.getdefaultlocale()
('zh_CN', 'cp936')123

示例

import locale
cmd = cmd.encode(locale.getdefaultlocale()[1])
subprocess.Popen(cmd)

作者:w妙音
来源:CSDN
原文:https://blog.csdn.net/wyongqing/article/details/70410999?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!

↑ 这个我没看太明白 不过还好还有一个例子↓

import subprocess
import sys
cmd = "cmd.exe"
begin = 101
end = 102
while begin < end:
    ##blow for windows shell chinese show##
    #reload(sys)
    #sys.setdefaultencoding('utf-8')
    print "excution result start :\n"
    child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    print "ping -n 1 -w 100 192.168.1." + str(begin) + "\n"
    child.stdin.write("ping -n 1 -w 100 192.168.1." + str(begin) + "\n")
    child.stdin.close()
    child.wait()
    print "excution result end:\n"
    begin += 1
    #print child.stdout.readlines()
    for line in child.stdout.readlines():
        ##blow for pycharm and cygwin show chinese#
        output = line.decode('cp936').encode('utf-8')
        print "%s\n" % output
Cloudox_ @Cloudox_

例子看不看都行,最重要的一句是

output = line.decode('cp936').encode('utf-8')

我在用完这一句后就神清气爽了↓

def run_command(ex):
    output = ''
    try:
        output = subprocess.check_output(ex, stderr=subprocess.STDOUT, shell=True)
    except:
        print("error")
    # print(output)
    out = output.decode('cp936').encode('utf-8')
    return out

相关文章

网友评论

      本文标题:python3 subprocess.check_output(

      本文链接:https://www.haomeiwen.com/subject/xurmaftx.html