Package: yawinpty
yet another winpty binding for pythoninstallpipinstallyawinptybuild from sourcepython 3.5+installVisual C++ 2015 Build Tools, then usepython setup.py buildto buildolder pythonVisual C++CPython version10.03.3, 3.49.02.6, 2.7, 3.0, 3.1, 3.2installbothVisual C++ 2015 Build Toolsand the matching version of Visual C++ Build Tools. open “Visual C++2015Build Tools Command Prompt” with the same arch as python, then usepython setup.py buildto buildbasic examplesget output from processfromyawinptyimport*# open a ptywithPty()aspty:# spawn a process ``python -c "print('HelloWorld!')"``pty.spawn(SpawnConfig(SpawnConfig.flag.auto_shutdown,cmdline='python -c "print(\'HelloWorld!\')"'))# open the out pipe of console to readwithopen(pty.conout_name(),'r')asf:# HelloWorld!print(f.read())communicate with processfromyawinptyimport*withPty()aspty:# spawn python replpty.spawn(SpawnConfig(SpawnConfig.flag.auto_shutdown,cmdline='python'))# open the in pipe of console to writewithopen(pty.conin_name(),'w')asf:f.write('1 + 2\n')# write EOF to exit pythonf.write('\x1a\n')withopen(pty.conout_name(),'r')asf:print(f.read())gui log of console program (navie)fromsysimportargvfromthreadingimportThreadfromsubprocessimportlist2cmdlinefromtkinterimport*fromyawinptyimport*root=Tk()con=Text(root)con.pack()defpoll():# strip escape seqwithPty(Config(Config.flag.plain_output))aspty:# run the cmdline passed in by ``sys.argv``pty.spawn(SpawnConfig(SpawnConfig.flag.auto_shutdown,cmdline=list2cmdline(argv[1:])))withopen(pty.conout_name(),'r')asf:whileTrue:ln=f.readline()ifnotln:break# log to guicon.insert(END,ln)Thread(target=poll).start()root.mainloop()usingyawinptythe common goal to useyawinptyis to open a pseudo terminal then spawn a process in it and send input to it’s stdin and get output from it’s stdout. yawinpty.Pty wrapper a pseudo-terminal and do the jobsclassyawinpty.Pty(config=yawinpty.Config())yawinpty.Pty accept a instance of yawinpty.Config as its configclassyawinpty.Config(*flags)for the flags to init a “config class” is commonly a set of Class.flag.*. example:cfg=yawinpty.Config(yawinpty.Config.flag.plain_output)help(yawinpty.Config.flag)for more supported flagsforyawinpty.SpawnConfigit’s similarhelp(yawinpty.Config)for more methodsinstances of thePtyclass have the following methods:Pty.conin_name()Pty.conout_name()Pty.conerr_name()get the name of console in/out/err pipe. the name could be passed to builtinopento open the pipePty.agent_process_id()get the process id of the agent processPty.set_size()set window size of the terminalPty.spawn(spawn_config)spawn a process in the pty. spawn_config is a instance ofyawinpty.SpawnConfig. note that one Pty instance could only spawn once otherwiseyawinpty.RespawnErrorwould be raisedreturns a tuple ofprocess id, thread idof spawned processclassyawinpty.SpawnConfig(*spawnFlags, appname=None, cmdline=None, cwd=None, env=None)spawnFlagsthe flags fromyawinpty.SpawnConfig.flagappnamefull path to executable file. can beNoneifcmdlineis specifiedcmdlinecommand line passed to the spawned processcwdworking directory for the spawned processenvthe environ for the spawned process, a dict like{'VAR1': 'VAL1', 'VAR2': 'VAL2'}note that init aSpawnConfigdoes notspawn a process. a process is spawned only when callingPty.spawn(). one SpawnConfig instance could be used multitimesPty.wait_agent(timeout = yawinpty.INFINITE)Pty.wait_subprocess(timeout = yawinpty.INFINITE)wait for agent/spawned process to exit. raise yawinpty.TimeoutExpired if out of timeoutPty.close()kill processes not exited, close pty and release Windows resourceexceptionsall winpty related exceptions are subclasses ofyawinpty.YawinptyError.help(yawinpty)for more information