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
20200525
有些东西,分享就好
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异