python标准输入,Python StringIO-选择性地将数据放入标准输入

We're using a bit of compiled python code that we don't have the source to. The code prompts for user input and we're trying to automate that portion.

Basically asks for username, password, then some various questions depending on certain circumstances. I don't know whether the compiled function is using raw_input, input or something else.

I've been able to use StringIO to replace stdin w/ the username and password and I can replace stdout with my own class and figure out which prompt is coming up, but I'm stumped when it comes to selectively placing data into stdin based on what I read from stdout.

import sys

import re

from StringIO import StringIO

def test():

overwrite = raw_input("The file exists, overwrite? ")

notify = raw_input("This file is marked for notifies. Notify?")

sys.stdout.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):

def __init__(self):

pass

def write(self, msg):

if re.search("The file exists, overwrite", msg):

# put data into stdin

if re.search("The file is marked for notification", msg):

# put data into stdin

sys.stdout = Catcher()

test()

I can't just preload a StringIO object, because the questions may vary depending on the circumstances, but I need to automate feeding into stdin because they're trying to put this into an automated build system, so they'll be providing the defaults via the command line to answer any questions that occur.

If I set stdin to an empty StringIO object before calling the compiled function, then it just errors out with EOF - not sure how to make it wait for input.

Something like this:

import sys

import re

from StringIO import StringIO

def test():

overwrite = raw_input("The file exists, overwrite? ")

notify = raw_input("This file is marked for notifies. Notify?")

sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):

def __init__(self, stdin):

self.stdin = stdin

def write(self, msg):

if re.search("The file exists, overwrite", msg):

self.stdin.write('yes\n')

if re.search("The file is marked for notification", msg):

self.stdin.write('no\n')

sys.stdin = StringIO()

sys.stdout = Catcher(sys.stdin)

test()

Produces:

Traceback (most recent call last):

File "./teststdin.py", line 25, in

test()

File "./teststdin.py", line 8, in test

overwrite = raw_input("The file exists, overwrite? ")

EOFError: EOF when reading a line

Any ideas?

解决方案

If you want to read from a StringIO you've just written to, you have to rewind it first to the position you've started writing.

Also, your second search tests for the wrong string.

This should work:

import sys

import re

from StringIO import StringIO

def test():

overwrite = raw_input("The file exists, overwrite? ")

notify = raw_input("This file is marked for notifies. Notify?")

sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):

def __init__(self, stdin):

self.stdin = stdin

def write(self, msg):

if re.search("The file exists, overwrite?", msg):

self.stdin.truncate(0)

self.stdin.write('yes\n')

self.stdin.seek(0)

if re.search("This file is marked for notifies. Notify?", msg):

self.stdin.truncate(0)

self.stdin.write('no\n')

self.stdin.seek(0)

sys.stdin = StringIO()

sys.stdout = Catcher(sys.stdin)

test()

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值