Python StringIO-选择性地将数据放入标准输入 [英] Python StringIO - selectively place data into stdin
问题描述
我们正在使用一些没有源代码的已编译python代码.该代码提示用户输入,我们正在尝试使该部分自动化.
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.
基本上要求输入用户名,密码,然后根据某些情况询问一些其他问题.我不知道编译后的函数是否正在使用raw_input,input或其他东西.
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.
我已经能够使用StringIO替换带有用户名和密码的stdin,并且可以使用自己的类替换stdout并确定出现哪个提示,但是在选择性地放置数据时,我感到很困惑根据我从stdout读取的内容进入stdin.
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()
我不能只预加载一个StringIO对象,因为问题可能会因情况而异,但是我需要自动将其输入标准输入,因为他们正试图将其输入到自动化构建系统中,因此通过命令行提供默认值,以回答出现的任何问题.
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.
如果在调用编译函数之前将stdin设置为空的StringIO对象,则EOF只会出错-不确定如何使其等待输入.
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.
类似这样的东西:
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()
产生:
Traceback (most recent call last):
File "./teststdin.py", line 25, in <module>
test()
File "./teststdin.py", line 8, in test
overwrite = raw_input("The file exists, overwrite? ")
EOFError: EOF when reading a line
有什么想法吗?
推荐答案
如果要从刚刚写的StringIO
中读取内容,则必须先将其倒回到开始写的位置.
另外,您的第二次搜索会测试错误的字符串.
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.
这应该有效:
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()
这篇关于Python StringIO-选择性地将数据放入标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!