37,721
社区成员
发帖
与我相关
我的任务
分享
import subprocess
import sys
import threading
import time
class mythread(threading.Thread):
def __init__(self,threadID):
threading.Thread.__init__(self)
self.threadID=threadID
def run(self):
t = time.localtime()
print("the xian start :%s the time:%s" % (self.threadID,time.asctime(t)))
# lock_thread.acquire()
if self.threadID=="01":
event()
else:
event2()
# t=time.localtime()
print("the xian end :%s the time:%s" % (self.threadID,time.asctime(t)))
def write(msg): # write负责将输入的命令转化为cmd能看懂的
temp_biaoshi.stdin.write((msg + "\r\n").encode('GBK'))
temp_biaoshi.stdin.flush()
def event():
# write("ping www.baidu.com")
# write("echo Hello!World 你好中国!你好世界!")
write("dir")
write("exit")
read_txt=temp_biaoshi.stdout.readline
while True:
line = read_txt()
if not line:
break
try:
tmp=line.decode("GBK")
sys.stdout.write(tmp)
except UnicodeDecodeError:
print("错误!")
def event2():
write("ping www.baidu.com")
write("echo Hello!World 你好中国!你好世界!")
# write("dir")
write("exit")
read_txt = temp_biaoshi.stdout.readline
while True:
line = read_txt()
if not line:
break
try:
tmp = line.decode("GBK")
sys.stdout.write(tmp)
except UnicodeDecodeError:
print("错误!")
if __name__=="__main__":
temp_biaoshi=subprocess.Popen("cmd.exe",shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
lock_thread=threading.Lock()
thread1=mythread("01")
thread2=mythread("02")
thread1.start()
thread1.join()
time.sleep(1)
thread2.start()
thread2.join()