import time
import os
import subprocess
import threading
class Grant:
def __init__(self):
self.id = os.getpid()
self.time = time.time()
self.p = None
self.end_time = time.time()
def grant(self, command):
self.p = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
)
self.p.stdin.write("ls\n".encode("utf-8"))
self.p.stdin.flush()
start_time = time.time()
# t1 = threading.Thread(target=self.next_command)
# t1.start()
t2 = threading.Thread(target=self.get_time)
t2.start()
while True:
content_open = self.p.stdout.readline()
content_open = (content_open.decode()).strip()
print("fist_mand:",content_open)
def get_time(self):
while True:
self.end_time = time.time()
time.sleep(0.1)
self.next_command()
self.kill_pid()
def next_command(self):
if (self.end_time - self.time) > 1:
print("come in next_command ...............")
self.p.stdin.write("cd data/local/tmp \n".encode("utf-8"))
self.p.stdin.flush()
self.p.stdin.write("ls \n".encode("utf-8"))
self.p.stdin.flush()
while True:
content_open = self.p.stdout.readline()
content_open = (content_open.decode()).strip()
print("data/local/tmp : ",content_open)
def kill_pid(self):
if (self.end_time - self.time) > 3:
os.system(f"kill {self.id}")
if __name__ == "__main__":
command = "adb shell"
test = Grant()
test.grant(command)
subprocess进程交互
最新推荐文章于 2024-03-15 14:27:30 发布