pywinpty - python windows伪终端实现终端自动化

pywinpty · PyPI

# High level usage using `spawn`
from winpty import PtyProcess

proc = PtyProcess.spawn('python')
proc.write('print("hello, world!")\r\n')
proc.write('print("ri zhao xiang lu sheng zi yan!")\r\n')
proc.write('print("yao kan pu bu gua qian chuan!")\r\n')
# proc.write('exit()\r\n')
# while proc.isalive():
print(proc.read())
proc.write('print("111111111111111")\r\n')
# proc.write('exit()\r\n')
# while proc.isalive():
print(proc.read())

# Low level usage using the raw `PTY` object
from winpty import PTY

# Start a new winpty-agent process of size (cols, rows)
cols, rows = 80, 25
process = PTY(cols, rows)

# Spawn a new console process, e.g., CMD
process.spawn(r'C:\windows\system32\cmd.exe')

# Read console output (Unicode)
process.write("ipconfig\r\n")

# Write input to console (Unicode)


# Resize console size
# new_cols, new_rows = 90, 30
# process.set_size(new_cols, new_rows)
#
# # Know if the process is alive
# alive = process.isalive()
# process.read()
print(process.read())
process.write('ipconfig\r\n')
print(process.read())
print("====",process.read())
process.write('hostname\r\n')
print(process.read())
# print(process.read())
# print(process.read())
# End winpty-agent process
# del process

 

posted @ 2025-06-13 13:59  我的腹肌不见了  阅读(109)  评论(0)    收藏  举报