关于子进程:python控制台和来自ping的文本输出,包括

Python console and text output from Ping including

本问题已经有最佳答案,请猛点这里访问。

我不知道发生了什么,但当我打印到控制台或文本文件时,换行符()不起作用,而是显示在字符串中。知道如何在控制台和文本文件中避免这种情况吗?

我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess

hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()

for line in lines:
    line = line.strip()
    ping = subprocess.Popen(["ping","-n","3",line],stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    out, error = ping.communicate()
    out = out.strip()
    error = error.strip()
    output = open("PingResults.txt",'a')
    output.write(str(out))
    output.write(str(error))
    print(out)
    print(error)
hosts_file.close()

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
b'Pinging 192.168.0.1 with 32 bytes of data:

Request timed out.

Request ti
med out.

Request timed out.



Ping statistics for 192.168.0.1:

    Pa
ckets: Sent = 3, Received = 0, Lost = 3 (100% loss),'

b''
b'Pinging 192.168.0.2 with 32 bytes of data:

Request timed out.

Request ti
med out.

Request timed out.



Ping statistics for 192.168.0.2:

    Pa
ckets: Sent = 3, Received = 0, Lost = 3 (100% loss),'

b''
b'Pinging 192.168.0.3 with 32 bytes of data:

Request timed out.

Request ti
med out.

Request timed out.



Ping statistics for 192.168.0.3:

    Pa
ckets: Sent = 3, Received = 0, Lost = 3 (100% loss),'

b''
b'Pinging 192.168.0.4 with 32 bytes of data:

Request timed out.

Request ti
med out.

Request timed out.



Ping statistics for 192.168.0.4:

    Pa
ckets: Sent = 3, Received = 0, Lost = 3 (100% loss),'

b''
b'Pinging 192.168.0.5 with 32 bytes of data:

Request timed out.

Request ti
med out.

Reply from 3.112.3.214: Destination host unreachable.



Ping st
atistics for 192.168.0.5:

    Packets: Sent = 3, Received = 1, Lost = 2 (66%
loss),'

b''

主机文件:

1
2
3
4
5
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5


1
2
3
4
5
6
7
8
9
10
11
12
import subprocess

hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()
hosts_file.close()

for line in lines:
    ping = subprocess.Popen(["ping","-n","3",line.strip()], stdout=subprocess.PIPE, stderr=subprocess.POPEN)
    with open('PingResults.txt', 'ab') as fh:
        for line in ping.stdout.readlines():
            fh.write(line)
    ping.stdout.close()

给我:

1
2
3
4
5
6
7
8
9
10
11
12
13
[torxed@faparch ~]$ python test.py && cat PingResults.txt
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.035 ms

--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.016/0.024/0.035/0.009 ms
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.

--- 192.168.0.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2008ms


问题是,您试图打印出一个python 3 bytes对象,而python无法自动转换为str对象,因为它无法确定字符编码是什么。

您必须将它转换为字符串,告诉python编码是什么,使用bytes对象的decode()方法…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess

hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()

for line in lines:
    line = line.strip()
    ping = subprocess.Popen(["ping","-n","3",line],stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    out, error = ping.communicate()
    out = out.strip()
    error = error.strip()
    output = open("PingResults.txt",'a')
    output.write(str(out))
    output.write(str(error))
    print(out.decode('utf-8'))
    print(error.decode('utf-8'))
hosts_file.close()