cmd和图形界面同时存在,且cmd命令一直不退出,如何和cmd命令交互的问题

# -*- coding: utf-8 -*-

"""
Module implementing Dialogimage.
"""

from PyQt5.QtCore import pyqtSlot, QThread, pyqtSignal
from PyQt5.QtWidgets import QDialog, QApplication

from  subprocess import Popen, PIPE, call, run
import subprocess
import threading

from Ui_image import Ui_Dialog
fp = open("ping.txt", 'w')
outfileno=fp.fileno()

fpin = open("input.txt", 'r')
infileno=fpin.fileno()

def cmdTread():
    #cmd = "ping  -t www.baidu.com"
    
   
    cmd=['ping','-t','www.baidu.com']
    proc=Popen(cmd, stdout=outfileno, shell=True,  universal_newlines=True)
    # 此处的stdout=outfileno为最关键的部分,将输出到文件中。由于ping一直运行,此时communicate并不能返回获取到输出信息,因而输出到文件中。 同时对于输入的交互,用什么命令测试下呢?
   try:
        out, err=proc.communicate(input=infileno, timeout=2)
    except subprocess.TimeoutExpired:
        proc.kill()
        out, err=proc.communicate()
        print("timeout")
    print(out)
    print(err)
    print(cmd)
    #os.system(cmd)
class Speech_Recognition_Thread(QThread):
    finished_signal = pyqtSignal(str)

    def __init__(self, parent = None):
        super().__init__(parent)
    
    def run(self):

        
        cmdTread()
        self.finished_signal.emit('over')
        
        
class Dialogimage(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(Dialogimage, self).__init__(parent)
        self.setupUi(self)
        
    def _show_message(self, message):
        print('message:', message)
        
    @pyqtSlot()
    def on_pushButton_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
       # raise NotImplementedError
       # cmdTread()
        self.speech = Speech_Recognition_Thread()
        self.speech.finished_signal.connect(self._show_message)
        self.speech.start()
    
    @pyqtSlot(bool)
    def on_g3_toggled(self, checked):
        """
        Slot documentation goes here.
        
        @param checked DESCRIPTION
        @type bool
        """
        # TODO: not implemented yet
        raise NotImplementedError

    

        
        
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
   
    ui = Dialogimage()
    
    ui.show()
    sys.exit(app.exec_())

   

proc=Popen(cmd,stdin=PIPE, stdout=outfileno,  universal_newlines=True)
proc.stdin.flush()


 proc.stdin.write('3\n')
    proc.stdin.write('5\n')


缺少回车\n是不行的。universal_newlines为true,如果此值为false,则需要 proc.stdin.write(b'3\n')


主要需求:

     1) windows下 运行某个不会立即返回的命令,例如ping -t xxx,如何在python代码中和此命令交互:获取命令输出,并给命令新的输入值。

   百度到点资料,后续看看。

   https://stackoverflow.com/questions/16091112/python-pipe-to-popen-stdin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

proware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值