The answer I'm writing below deviates a bit from writing to file. I'm using a multiprocessing.Queue (which internally it uses a Pipe, which uses a file to communicate the processes), but from a programatic point of view, it looks like it doesn't (I don't know if this is what you want or not). If you want to go with this solution, you should take a look to the documentation of the multiprocessing module.
You can certainly implement your own inter-process communication system, if you prefer, but once you start doing multi-threaded stuff, things get ugly. If it's multi-processed, things get... well... much, much uglier, so I'd go with something that exists out there, is well tested... yadda yadda yadda
I'd make this my typer.py
:
def main(q):
while True:
print "Yellou. User typed %s" % q.get()
And this my 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()
To be able to import typer
in lock.py
these two scripts musts live in a directory that contains a third python file called __init__.py
. This file can be totally empty, but I'll tell python that it's currently in a package (see this and this).
Your directory structure should look something like this:
my_programs/
|> typer.py
|> lock.py
|> __init__.py
If you run your lock.py
, this will happen:
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
As I mentioned, I'm not sure if this is what you're looking for exactly.
EDIT (I think the OP was trying to use typer.py
to find the number that unlocks "the program")
If what you want is simulate a user interaction with the lock.py
, I suggest you look at pexpect. As far as I know, is multiplatform:
This would be your 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()
That should find your number:
Trying 669
669 didn't work
Trying 670
670 didn't work
Trying 671
Cracked. Num is 671
Got 671