python---命令行程序的自动化交互

keywords: python 命令行程序 自动化 交互

以这个python程序为例:

# coding:utf8
# python3

import random

first_num = str(random.randint(0, 10))
second_num = str(random.randint(0, 10))

first_input = input('input {}: '.format(first_num))
second_input = input('input {}: '.format(second_num))

if first_num==first_input and second_num==second_input:
    print('Right!')
else:
    print('Wrong!')

用脚本调用这个程序,并自动输出 ‘Rignt!’,使用 subprocess 实现:

# coding:utf-8
# python3

import sys
import re
import subprocess
from threading import Thread


class ProcessInteraction:
    process = None

    def __init__(self, command):
        self.process = subprocess.Popen(
            command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

    def __del__(self):
        self.process.stdin.close()
        self.process.stdout.close()

    def recvline(self):
        return self.process.stdout.readline().decode('utf8')

    def recv_n(self, n):
        return self.process.stdout.read(n).decode('utf8')

    def recvuntil(self, want_end_str):
        current_str = ''
        while True:
            current_str += self.recv_n(1)
            if current_str.endswith(want_end_str):
                return current_str

    def recvuntil_re(self, want_re_str):
        current_str = ''
        while True:
            current_str += self.recv_n(1)
            s = re.search(want_re_str, current_str)
            if s:
                return [current_str, s]

    def send(self, send_str):
        final_bytes = send_str.encode('utf8')
        self.process.stdin.write(final_bytes)
        self.process.stdin.flush()

    def sendline(self, send_str):
        self.send(send_str+'\n')
    
    def interact(self):
        def recv_loop():
            while True:
                c = self.recv_n(1)
                # print 写到控制台会有延时,直接用系统io写
                sys.stdout.write(c)
                sys.stdout.flush()

        def send_loop():
            while True:
                send_str = input()
                self.sendline(send_str)

        recv_thread = Thread(target=recv_loop)
        send_thread = Thread(target=send_loop)

        recv_thread.start()
        send_thread.start()

        recv_thread.join()
        send_thread.join()


command = ['py', '-3', 'want_right.py']

pi = ProcessInteraction(command)
for i in range(2):
    content = pi.recvuntil_re(r'input (\d+): ')
    print(content[0])
    num = content[1].group(1)
    print(repr(num))
    pi.sendline(num)
content = pi.recvline()
print(content)

注意:如果在 linux 下执行,command 需要修改为 ['python3', 'want_right.py']

输出:

input 5:
'5'
input 0:
'0'
Right!

感谢 zl

&&&&&&& 貌似只对python脚本好用,exe和elf都不行,读不到输出,问题待解决

20200525

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python-docx-0.8.11.whl是一个Python第三方库,主要用于创建、修改和解析Microsoft Word文档。在Python和Microsoft Office之间进行转换和通信,使用Python-docx-0.8.11.whl可以轻松地使用Python操作Word文档。 下载Python-docx-0.8.11.whl可以通过访问Python-docx的官方网站或使用pip来直接安装。首先需要确认Python环境是否正确,并且安装了pip工具。然后在命令行中输入 pip install python-docx 即可完成安装。如果需要使用特定版本,也可以从官网下载对应版本的whl文件,然后通过 pip install python-docx-0.8.11.whl 进行安装。 使用Python-docx-0.8.11.whl可以实现许多功能,例如创建、修改和格式化Word文档,添加表格、图片、超链接等元素,以及读取和处理Word文档的内容和属性。此外,Python-docx-0.8.11.whl还提供了丰富的文档生成和处理工具,能够帮助开发者更高效地完成工作。 ### 回答2: Python-docx是一款用于编写和读取Microsoft Word文件(docx)的Python库。Python-docx-0.8.11.whl是Python-docx库的一个版本,其中“.whl”文件扩展名表示该版本可作为Python包进行安装。 要下载Python-docx-0.8.11.whl,可以在Python-docx库的官方网站或Python Package Index(PyPI)上找到该版本的下载链接。一般来说,可以在下载页面上找到与操作系统匹配的版本并下载。 下载后,将.whl文件保存到你的计算机上,在命令行终端中使用pip安装该包,首先需确保你已经安装了Python和pip。在命令行输入“pip install <path to python-docx-0.8.11.whl>”并按回车键,等待安装完成后,Python-docx-0.8.11.whl与其他Python库一样,可以在你的Python程序中使用。 Python-docx-0.8.11.whl提供了许多便捷的API和功能,可以帮助Python开发人员更轻松地与Word文件进行交互。例如,开发人员可以创建新的Word文档或打开现有的文件,读取和操作文档内容,添加图形、表格或列表等等。如此便捷的处理方式,使Python-docx-0.8.11.whl成为编写和处理Word文件的首选库。 ### 回答3: Python-docx-0.8.11.whl是Python-docx的一个特定版本,其中.whl表示Python的安装包格式。Python-docx是一个用于创建和修改Microsoft Word文档的Python库。使用Python-docx,可以通过Python脚本自动化创建和修改Word文档,使得简化了文本处理的工作流程。 为了使用Python-docx库,需要安装它,并且PyPI是一个基于Python的软件仓库,可以提供Python软件包的下载。Python-docx-0.8.11.whl是这个库的安装包,可以使用pip命令安装或是手动下载进行安装。 下载Python-docx-0.8.11.whl可以从其官方网站下载,或是从任一类PyPI的软件库网站直接下载。下载完成后,使用pip install Python-docx-0.8.11.whl或者是pip install下载的文件路径即可完成安装。 总而言之,Python-docx-0.8.11.whl是一个Python-docx库的安装包,可以通过pip安装或手动下载安装。它极大程度上简化了编码创建和修改Word文档的流程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值