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

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

  • <legend id='Kyhe3'><style id='Kyhe3'><dir id='Kyhe3'><q id='Kyhe3'></q></dir></style></legend>

        <tfoot id='Kyhe3'></tfoot>
          <bdo id='Kyhe3'></bdo><ul id='Kyhe3'></ul>

        如何从 Python subprocess.check_output() 捕获异常输出?

        时间:2023-07-22
          <bdo id='P22xg'></bdo><ul id='P22xg'></ul>

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

            <tbody id='P22xg'></tbody>

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

            <legend id='P22xg'><style id='P22xg'><dir id='P22xg'><q id='P22xg'></q></dir></style></legend>

                • 本文介绍了如何从 Python subprocess.check_output() 捕获异常输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 Python 中进行比特币支付.在 bash 中,我通常会这样做:

                  I'm trying to do a Bitcoin payment from within Python. In bash I would normally do this:

                  bitcoin sendtoaddress <bitcoin address> <amount>
                  

                  例如:

                  bitcoin sendtoaddress 1HoCUcbK9RbVnuaGQwiyaJGGAG6xrTPC9y 1.4214
                  

                  如果成功,我会得到一个交易 ID 作为输出,但如果我尝试转移一个大于我的比特币余额的金额,我会得到以下输出:

                  If it is successful I get a transaction id as output, but if I try to transfer an amount larger than my bitcoin balance, I get the following output:

                  error: {"code":-4,"message":"Insufficient funds"}
                  

                  在我的 Python 程序中,我现在尝试按如下方式付款:

                  In my Python program I now try to do the payment as follows:

                  import subprocess
                  
                  try:
                      output = subprocess.check_output(['bitcoin', 'sendtoaddress', address, str(amount)])
                  except:
                      print "Unexpected error:", sys.exc_info()
                  

                  如果有足够的余额,它可以正常工作,但如果没有足够的余额,sys.exc_info() 会打印出来:

                  If there's enough balance it works fine, but if there's not enough balance sys.exc_info() prints out this:

                  (<class 'subprocess.CalledProcessError'>, CalledProcessError(), <traceback object at 0x7f339599ac68>)
                  

                  它不包括我在命令行上遇到的错误.所以我的问题是;如何从 Python 中获取输出的错误({code":-4,message":Insufficient fund"})?

                  It doesn't include the error which I get on the command line though. So my question is; how can I get the outputted error ({"code":-4,"message":"Insufficient funds"}) from within Python?

                  推荐答案

                  根据subprocess.check_output() docs,错误引发的异常有一个 output 属性,可用于访问错误详细信息:

                  According to the subprocess.check_output() docs, the exception raised on error has an output attribute that you can use to access the error details:

                  try:
                      subprocess.check_output(...)
                  except subprocess.CalledProcessError as e:
                      print(e.output)
                  

                  然后您应该能够分析此字符串并使用 json 模块解析错误详细信息:

                  You should then be able to analyse this string and parse the error details with the json module:

                  if e.output.startswith('error: {'):
                      error = json.loads(e.output[7:]) # Skip "error: "
                      print(error['code'])
                      print(error['message'])
                  

                  这篇关于如何从 Python subprocess.check_output() 捕获异常输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python的子进程不允许使用shell = True进行进程替换? 下一篇:在 python 脚本中启动 shell 命令,等待终止并返回脚本

                  相关文章

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

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

                  <legend id='q7sob'><style id='q7sob'><dir id='q7sob'><q id='q7sob'></q></dir></style></legend>

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