刚接触编程并开始了一个业余项目。在 Python 3.4 中,我将一个程序放在一起,将数字输入到另一个充当组合锁的 Python 程序中。组合锁读取 3 位数字组合 000-999,如果用户或第二个 Python 程序键入正确的 3 位数字组合则解锁。我已经使用 from win32api import keybd_event 完成了 typer 程序,并且具有支持它的功能。
打字机程序片段:
def main():
os.startfile('lockprogram.py')
for num in range(0,1000):
Write(str(num).zfill(3),speed = 1000)
Press('ENTER')
main()
这是锁程序:
def main():
start = 'locked'
while start =='locked':
password = str(input('Enter the numbered three digit password: '))
if password == str(671).zfill(3):
start = 'open'
input('You unlocked the program. Enter to continue. ')
else:
print('Incorrect.')
#End program response.
x = True
while x == True:
response = str(input('To end press q: '))
if response == 'q':
x = False
else:
print()
main()
我希望打字机程序专门写入 lockprogram.py,而不仅仅是在打开时按下键盘输入。无论如何要做到这一点?像 py_file.write() 一样?
请您参考如下方法:
我在下面写的答案与写入文件有点不同。我正在使用 multiprocessing.Queue (它在内部使用 Pipe ,它使用文件来传达进程),但从编程的角度来看,它看起来不像(我不知道这是否是你想要的) .如果您想使用此解决方案,您应该查看 multiprocessing 的文档。模块。
如果您愿意,您当然可以实现自己的进程间通信系统,但是一旦您开始使用多线程,事情就会变得很糟糕。如果它是多重处理的,事情会变得......好吧......非常非常丑陋,所以我会选择已经存在的,经过良好测试的东西......yadda yadda yadda
我会把它设为我的 typer.py :
def main(q):
while True:
print "Yellou. User typed %s" % q.get()
这是我的 lock.py :
from multiprocessing import Process, Queue
import typer
def main():
start = 'locked'
while start == 'locked':
password = str(
input('Enter the numbered three digit password: ')
).zfill(3)
print "You entered %s" % password
if password == '671':
start = 'open'
input('You unlocked the program. Enter to continue. ')
else:
print('Incorrect.')
# End program response.
# Launch typer.py
q = Queue()
p = Process(target=typer.main, args=(q,))
p.start()
x = True
while x is True:
response = input('To end press q: ')
if response == 'q':
x = False
p.terminate()
else:
q.put(response)
if __name__ == "__main__":
main()
能够import typer在lock.py这两个脚本必须位于包含第三个 python 文件的目录中 __init__.py .这个文件可以完全是空的,但我会告诉 python 它当前在一个包中(参见 this 和 this)。
你的目录结构应该是这样的:
my_programs/
|> typer.py
|> lock.py
|> __init__.py
如果您运行 lock.py ,这将发生:
Enter the numbered three digit password: 671
You entered 671
You unlocked the program. Enter to continue.
Here
To end press q: helou
To end press q: Yellou. User typed helou
howdy?
To end press q: Yellou. User typed howdy?
q
正如我提到的,我不确定这是否正是您要找的。
编辑(我认为 OP 试图使用 typer.py 找到解锁“程序”的数字)
如果您想要模拟与 lock.py 的用户交互,我建议你看看pexpect .据我所知,是多平台的:
这将是您的 typer.py
import pexpect
def main():
child = pexpect.spawnu('python /path/to/lock.py')
child.expect(u"Enter the numbered three digit password:.*", timeout=1)
pwd = None
for num in range(1000):
if pwd is None:
print "Trying %s" % (num)
child.sendline(unicode(num))
i = child.expect([
u'You entered.*\r\nIncorrect.*',
u'You entered.*\r\nYou unlocked the program.*',
pexpect.EOF])
if i == 0:
print "%s didn't work" % num
elif i == 1:
print "Cracked. Num is %s" % num
pwd = num
child.terminate(force=True)
else:
print "woot?"
return pwd
print "Got %s" % main()
那应该找到你的电话号码:
Trying 669
669 didn't work
Trying 670
670 didn't work
Trying 671
Cracked. Num is 671
Got 671
