<legend id='L5q4w'><style id='L5q4w'><dir id='L5q4w'><q id='L5q4w'></q></dir></style></legend>
<tfoot id='L5q4w'></tfoot>
        <bdo id='L5q4w'></bdo><ul id='L5q4w'></ul>
    1. <small id='L5q4w'></small><noframes id='L5q4w'>

        <i id='L5q4w'><tr id='L5q4w'><dt id='L5q4w'><q id='L5q4w'><span id='L5q4w'><b id='L5q4w'><form id='L5q4w'><ins id='L5q4w'></ins><ul id='L5q4w'></ul><sub id='L5q4w'></sub></form><legend id='L5q4w'></legend><bdo id='L5q4w'><pre id='L5q4w'><center id='L5q4w'></center></pre></bdo></b><th id='L5q4w'></th></span></q></dt></tr></i><div id='L5q4w'><tfoot id='L5q4w'></tfoot><dl id='L5q4w'><fieldset id='L5q4w'></fieldset></dl></div>

        Ping 的 Python 控制台和文本输出,包括

        时间:2023-09-03

        • <legend id='4EeWl'><style id='4EeWl'><dir id='4EeWl'><q id='4EeWl'></q></dir></style></legend>
            • <bdo id='4EeWl'></bdo><ul id='4EeWl'></ul>

                <tbody id='4EeWl'></tbody>
                <i id='4EeWl'><tr id='4EeWl'><dt id='4EeWl'><q id='4EeWl'><span id='4EeWl'><b id='4EeWl'><form id='4EeWl'><ins id='4EeWl'></ins><ul id='4EeWl'></ul><sub id='4EeWl'></sub></form><legend id='4EeWl'></legend><bdo id='4EeWl'><pre id='4EeWl'><center id='4EeWl'></center></pre></bdo></b><th id='4EeWl'></th></span></q></dt></tr></i><div id='4EeWl'><tfoot id='4EeWl'></tfoot><dl id='4EeWl'><fieldset id='4EeWl'></fieldset></dl></div>

                <small id='4EeWl'></small><noframes id='4EeWl'>

                <tfoot id='4EeWl'></tfoot>

                  本文介绍了Ping 的 Python 控制台和文本输出,包括 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I dont know what is happening, but when I am printing to the console or to a text file, the newline ( ) is not functioning but rather showing in the string. Any idea how to avoid this in both the console and the text file?

                  我的代码:

                  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()
                  

                  输出:

                  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''
                  

                  主机文件:

                  192.168.0.1
                  192.168.0.2
                  192.168.0.3
                  192.168.0.4
                  192.168.0.5
                  

                  推荐答案

                  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()
                  

                  给我:

                  [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
                  

                  这篇关于Ping 的 Python 控制台和文本输出,包括 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Heroku dyno 上运行多个进程是否可行? 下一篇:TypeError:需要一个类似字节的对象,而不是 subprocess.check_output 中的“str&quo

                  相关文章

                    <i id='7GDSu'><tr id='7GDSu'><dt id='7GDSu'><q id='7GDSu'><span id='7GDSu'><b id='7GDSu'><form id='7GDSu'><ins id='7GDSu'></ins><ul id='7GDSu'></ul><sub id='7GDSu'></sub></form><legend id='7GDSu'></legend><bdo id='7GDSu'><pre id='7GDSu'><center id='7GDSu'></center></pre></bdo></b><th id='7GDSu'></th></span></q></dt></tr></i><div id='7GDSu'><tfoot id='7GDSu'></tfoot><dl id='7GDSu'><fieldset id='7GDSu'></fieldset></dl></div>

                    <small id='7GDSu'></small><noframes id='7GDSu'>

                      <tfoot id='7GDSu'></tfoot>
                    1. <legend id='7GDSu'><style id='7GDSu'><dir id='7GDSu'><q id='7GDSu'></q></dir></style></legend>
                        <bdo id='7GDSu'></bdo><ul id='7GDSu'></ul>