python stdin和stdout_无法使用Python写入和读取stdin / stdout

在MacOS上,使用Python 2.7通过子进程与PHP交互时遇到问题,无法正确写入stdin或从stdout读取数据。脚本尝试写入'hello world'到PHP的stdin,但只从stdout接收到空行和错误信息。
摘要由CSDN通过智能技术生成

我在MacOS 10.7.4上使用Python 2.7。这是一个较长的脚本(不是我写的)的一部分,它基本上为PHP提供了一些配置选项,将它们写入PHP的stdin,然后从它的stdout中读取它们。

实际上,我似乎无法从stdout中读取任何内容。下面的脚本应该写'hello world',但是它写的只是一个空行。

任何人都可以提出什么问题?我不太熟悉stdin和stdout在Python或PHP中的工作方式,所以我很难调试它。

php_path = '/usr/bin/php'

args = [php_path]

child = subprocess.Popen(args,

stdin=subprocess.PIPE,

stdout=subprocess.PIPE,

universal_newlines=True)

# Write to PHP stdin

print >>child.stdin, """

print "hello world\n";

# In reality we'd write some configuration options here,

# but in practice we can't even write 'hello world'.

}

?>"""

child.stdin.close()

# Read from PHP stdout

line = True

while line:

print 'line', line

line = child.stdout.readline()

print 'line from stdout', line

if line == "hello world\n":

break

else:

raise Exception, "%s: failed to read options" % (php_path)这个输出是:

line True

line from stdout

line

line from stdout Parse error: parse error in - on line 5

line Parse error: parse error in - on line 5

line from stdout

Traceback (most recent call last):

File "test.py", line 25, in

raise Exception, "%s: failed to read options" % (php_path)

Exception: /usr/bin/php: failed to read options/usr/bin/php绝对有一个PHP。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值