天天看點

【求助】python中subprocess向cmd發送指令行,怎麼發送ctrl+c的指令?

python3寫了一個subprocess向cmd發送指令行的程式,現在想在發送傳回過程中停止,就像是cmd發送ctrl+c,查了一些資料,說是發送0x03但是沒有反應啊

代碼如下:

sub = subprocess.Popen(cmd, shell = True ,bufsize = -1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, \

                               stderr=subprocess.STDOUT)

        while True :      

            if flag:

                print('flag:'+str(flag))

                sub.stdin.write(byte(0x03)) 

            datetime_dt = datetime.datetime.today()  # 擷取目前日期和時間

            datetime_str = datetime_dt.strftime("%Y/%m/%d %H:%M:%S.%f")  # 格式化日期時間

            resText = sub.stdout.readline().decode('gbk')

            if resText == '': 

                break  

            textLog.insert(tk.END,datetime_str+ ' -| INFO - | ' +str(resText) )