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

      <small id='XvHP5'></small><noframes id='XvHP5'>

    1. <legend id='XvHP5'><style id='XvHP5'><dir id='XvHP5'><q id='XvHP5'></q></dir></style></legend>
        <bdo id='XvHP5'></bdo><ul id='XvHP5'></ul>

      1. Python 'subprocess' CalledProcessError: Command '

        时间:2023-07-21
          <tbody id='cpXsK'></tbody>
        <legend id='cpXsK'><style id='cpXsK'><dir id='cpXsK'><q id='cpXsK'></q></dir></style></legend>
        • <bdo id='cpXsK'></bdo><ul id='cpXsK'></ul>
          <i id='cpXsK'><tr id='cpXsK'><dt id='cpXsK'><q id='cpXsK'><span id='cpXsK'><b id='cpXsK'><form id='cpXsK'><ins id='cpXsK'></ins><ul id='cpXsK'></ul><sub id='cpXsK'></sub></form><legend id='cpXsK'></legend><bdo id='cpXsK'><pre id='cpXsK'><center id='cpXsK'></center></pre></bdo></b><th id='cpXsK'></th></span></q></dt></tr></i><div id='cpXsK'><tfoot id='cpXsK'></tfoot><dl id='cpXsK'><fieldset id='cpXsK'></fieldset></dl></div>

              • <tfoot id='cpXsK'></tfoot>

                  <small id='cpXsK'></small><noframes id='cpXsK'>

                1. 本文介绍了Python 'subprocess' CalledProcessError: Command '[...]' 返回非零退出状态 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  正在执行以下脚本...

                  Executing the following script...

                  import socket                   
                  import sys                          
                  
                  from collections import OrderedDict
                  from subprocess import check_output
                  from threading import Thread    
                  

                  [...]

                  class IpCheck(Thread):  
                  
                      RECEIVED_PACKAGES_RE = re.compile(r'(d+) received')
                  
                      def __init__(self, ip):                         
                          Thread.__init__(self)
                          self.ip = ip
                          self.result = None
                  
                      def run(self):                          
                          match = self.RECEIVED_PACKAGES_RE.search(
                              check_output(['ping', '-q', '-c2', '-W1', self.ip])
                          )
                  
                          successful_ping_count = int(match.group(1)) if match else 0
                  
                          if successful_ping_count == 0:
                              self.result = 'no response'
                          elif successful_ping_count == 1:
                              self.result = 'alive, but 50% package loss'
                          elif successful_ping_count == 2:
                              self.result = check_snmp(self.ip)
                          else:
                              assert False
                  
                  [...]
                  

                  ...导致错误:

                  CalledProcessError:命令 '[ping', '-q', '-c2', '-W1', '10.81.3.80 ']' 返回非零退出状态 1

                  CalledProcessError: Command '[ping', '-q', '-c2', '-W1', '10.81.3.80 ']' returned non-zero exit status 1

                  check_output 中添加stderr = STDOUT"并没有产生任何有用的反馈.

                  Adding "stderr = STDOUT" in check_output did not produce any useful feedback.

                  如何获取有关错误的更多信息以便解决问题?

                  How can I obtain more information regarding the error so that I can troubleshoot it?

                  推荐答案

                  subprocess.check_output 在非零退出代码和 ping 上引发 CalledProcessErrorcode> 如果出现错误(例如,未知域名、站点已关闭、站点因某种原因阻止 ICMP 或您的 Internet 连接已关闭),则返回非零退出代码.

                  subprocess.check_output raises CalledProcessError on non-zero exit code, and ping returns non-zero exit code if something is wrong (e.g. unknown domain name, or site is down, or site has ICMP blocked for some reason, or your Internet connection is down).

                  如果您想检查输出和退出代码,请使用 subprocess.Popen:

                  If you want to examine both output and exit code, use subprocess.Popen:

                  import subprocess
                  import sys
                  
                  site = sys.argv[1]
                  ping_count = 4
                  process = subprocess.Popen(['ping', site, '-c', str(ping_count)],
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.STDOUT)
                  returncode = process.wait()
                  print('ping returned {0}'.format(returncode))
                  print(process.stdout.read())
                  

                  例子:

                  $ python ping.py google.com         <-- ping successful
                  ping returned 0
                  PING google.com (195.64.213.27) 56(84) bytes of data.
                  64 bytes from cache.google.com (195.64.213.27): icmp_seq=1 ttl=57 time=59.8 ms
                  64 bytes from cache.google.com (195.64.213.27): icmp_seq=2 ttl=57 time=2.43 ms
                  64 bytes from cache.google.com (195.64.213.27): icmp_seq=3 ttl=57 time=77.0 ms
                  64 bytes from cache.google.com (195.64.213.27): icmp_seq=4 ttl=57 time=43.8 ms
                  
                  --- google.com ping statistics ---
                  4 packets transmitted, 4 received, 0% packet loss, time 3004ms
                  rtt min/avg/max/mdev = 2.439/45.802/77.096/27.658 ms
                  
                  $ python ping.py asdasdas.com       <-- DNS resolved, but site is down
                  ping returned 1
                  PING asdasdas.com (69.172.201.208) 56(84) bytes of data.
                  
                  --- asdasdas.com ping statistics ---
                  4 packets transmitted, 0 received, 100% packet loss, time 3024ms
                  
                  $ python ping.py asdasdasdasda.com  <-- DNS failed
                  ping returned 2
                  ping: unknown host asdasdasdasda.com
                  

                  这篇关于Python 'subprocess' CalledProcessError: Command '[...]' 返回非零退出状态 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      <bdo id='O2Uhu'></bdo><ul id='O2Uhu'></ul>

                      <small id='O2Uhu'></small><noframes id='O2Uhu'>

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

                        • <tfoot id='O2Uhu'></tfoot>