为什么从蟒蛇这个庆典调用不行? [英] Why does this bash call from python not work?

查看:249
本文介绍了为什么从蟒蛇这个庆典调用不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用比特币瞎搞了一下。当我想获得当地的一些信息比特币安装,我只需运行比特币是getinfo 和我得到的是这样的:

  {
    版本:90100,
    protocolversion:70002,
    walletversion:60000,
    平衡:0.00767000,
    块:306984,
    timeoffset:0,
    连接:61,
    代理 : ,
    看病难:13462580114.52533913,
    testnet:假的,
    keypoololdest:1394108331,
    keypoolsize:101,
    paytxfee:0.00000000,
    错误:
}

我现在想从Python中做到这一点调用(之前有人指出出来,我知道有的Python对于比特币实现,我只是想了解这样做我自己)。所以我第一次尝试执行一个简单的 LS 这样的命令:

 导入子
流程= subprocess.Popen('ls'的,标准输出= subprocess.PIPE)
输出= process.communicate()[0]
的打印输出

这工作得很好,打印出的文件和文件夹列表预期。于是我做了这一点:

 导入子
流程= subprocess.Popen('比特币是getinfo',标准输出= subprocess.PIPE)
输出= process.communicate()[0]
的打印输出

但是这提供了以下错误:

 回溯(最后最近一次调用):
  文件testCommandLineCommands.py,2号线,上述<&模块GT;
    流程= subprocess.Popen('比特币是getinfo',标准输出= subprocess.PIPE)
  文件/usr/lib/python2.7/subprocess.py,线路710,在__init__
    errread,ERRWRITE)
  文件/usr/lib/python2.7/subprocess.py,1327线,在_execute_child
    提高child_exception
OSERROR:[错误2]没有这样的文件或目录

在这里我有点迷失。有谁知道什么是错在这里?所有的提示是欢迎!


利用出色的答案,下面我现在做了以下的功能,这可能会派上用场的其他人。它需要一个字符串,或带独立参数可迭代并解析输出,如果它是JSON:

 高清doCommandLineCommand(命令):
    流程= subprocess.Popen(指挥,标准输出= subprocess.PIPE,壳= isinstance(指挥,STR))
    输出= process.communicate()[0]
    尝试:
        返回json.loads(输出)
    除了ValueError错误:
        返回输出


解决方案

无论使用参数的顺序:

  =过程subprocess.Popen(['比特币','是getinfo'],标准输出= subprocess.PIPE)

或设置参数

  =过程subprocess.Popen('比特币是getinfo',标准输出= subprocess.PIPE,壳=真)

您可以找到<获得更多的信息href=\"https://docs.python.org/2/library/subprocess.html#frequently-used-arguments\">documentation:


  

ARGS所需的所有来电,应该是一个字符串,或者程序参数的序列。提供的参数的序列一般是preferred,因为它允许模块的任何所需的逸出和参数引用(例如,以允许在文件名中的空格)照顾。如果传递一个字符串,无论是外壳必须是真实的(见下文),否则该字符串必须简单地命名程序未指定任何参数执行。


I am messing around with Bitcoins a bit. When I want to get some info about the local bitcoin install, I simply run bitcoin getinfo and I get something like this:

{
    "version" : 90100,
    "protocolversion" : 70002,
    "walletversion" : 60000,
    "balance" : 0.00767000,
    "blocks" : 306984,
    "timeoffset" : 0,
    "connections" : 61,
    "proxy" : "",
    "difficulty" : 13462580114.52533913,
    "testnet" : false,
    "keypoololdest" : 1394108331,
    "keypoolsize" : 101,
    "paytxfee" : 0.00000000,
    "errors" : ""
}

I now want to do this call from within Python (before anyone points it out; I know there are Python implementations for Bitcoin, I just want to learn doing it myself). So I first tried executing a simple ls command like this:

import subprocess
process = subprocess.Popen('ls', stdout=subprocess.PIPE)
output = process.communicate()[0]
print output

This works fine, printing out the list of files and folders as expected. So then I did this:

import subprocess
process = subprocess.Popen('bitcoin getinfo', stdout=subprocess.PIPE)
output = process.communicate()[0]
print output

but this gives the following error:

Traceback (most recent call last):
  File "testCommandLineCommands.py", line 2, in <module>
    process = subprocess.Popen('bitcoin getinfo', stdout=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

and here I'm kinda lost. Does anybody know what's wrong here? All tips are welcome!

[EDIT] Using the excellent answers below I now made the following function, which might come in handy for others as well. It takes either a string, or an iterable with separate arguments and parses the output if it is json:

def doCommandLineCommand(command):
    process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=isinstance(command, str))
    output = process.communicate()[0]
    try:
        return json.loads(output)
    except ValueError:
        return output

解决方案

Either use a sequence in arguments:

process = subprocess.Popen(['bitcoin', 'getinfo'], stdout=subprocess.PIPE)

or set the shell parameter to True:

process = subprocess.Popen('bitcoin getinfo', stdout=subprocess.PIPE, shell=True)

You can find more information in the documentation:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

这篇关于为什么从蟒蛇这个庆典调用不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆