I'm writing a python script to automatically close an Android Emulator. I used to work on a Linux environments but I'm now migrating the code to Windows. Problem is,
$ adb emu kill
Doesn't work on Windows so I resort to making a python script that telnets to the emulator and kills the emulator. Here's the code:
import telnetlib
host = "localhost"
port = "5554"
tn = telnetlib.Telnet(host,port)
tn.write("kill\n")
tn.close()
The problem that I encountered with this is that it doesn't work when I try running this code when I enter
python killEmulator.py
with "killEmulator.py" being the filename of the code.
BUT when I enter the lines of this file one by one on the command line, it works and manages to kill the emulator.
import telnetlib
host = "localhost"
port = "5554"
tn = telnetlib.Telnet(host,port)
tn.write("kill\n")
tn.close()
When I do it like this, it works perfect. Can anyone tell what's going on?
I don't know the details here, but when you open a Telnet session the server needs to start a new shell process, and probably can't accept any data until after the shell has been started, depending on the server implementation.
A simple fix for your problem is to just add time.sleep(0.5)
before tn.write("kill\\n")
, giving the server half a second to get ready. A more elegant way would be to wait for the prompt before writing anything, like this:
r = tn.read_until("$ ", 5)
assert "$ " in r, "Timeout waiting for prompt!"
tn.write("kill\n")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.