Ich möchte eine Bash-Sitzung in Python öffnen und damit interagieren, als wäre es ein Terminal. Das ist, was ich habe, so weit:Wie öffne ich eine Bash-Sitzung mit Python und kommuniziere weiterhin damit?
import subprocess as s
class Session:
proc = None
def execute(self, cmd):
if self.proc is None:
self.proc = s.Popen("/bin/bash", shell=True, stdin=s.PIPE, stdout=s.PIPE, stderr=s.STDOUT)
cmd = cmd + ' ; echo EOF\n'
self.proc.stdin.write(cmd.encode())
output = ''
while not output.endswith("EOF"):
output += self.proc.stdout.read(1).decode()
return output
sess = Session()
sess.execute("export MY_VAR=HELLO")
sess.execute("git clone https://github.com/internetsadboy/crapi.git")
Doch während git clone ich diese Ausgabe zu erhalten, und es scheitert
Cloning into 'crapi'...
EOF
Haben Sie versucht, läuft/bin/bash? Vielleicht können Sie danach Befehle an sie weiterleiten. – dGRAMOP
Mögliches Duplikat: [Interaktive Eingabe/Ausgabe mit Python] (https://stackoverflow.com/questions/19880190/interactive-input-output-using-python). – John1024
Ich glaube nicht, dass 'Export MY_VAR = HELLO' in einem Subprozess funktioniert –