我正在使用pyxterm.js创建一个Web终端应用程序,目前我面临一个问题,即如何获取已创建的pty fd的当前路径(如os.getcwd)。
脚本:
app = Flask(__name__)
app.config["fd"] = None
app.config["child_pid"] = None
socketio = SocketIO(app)
def read_and_forward_pty_output():
max_read_bytes = 1024 * 20
while True:
socketio.sleep(0.01)
if app.config["fd"]:
timeout_sec = 0
(data_ready, _, _) = select.select([app.config["fd"]], [], [], timeout_sec)
if data_ready:
output = os.read(app.config["fd"], max_read_bytes).decode("utf8", "ignore")
@app.route("/")
def index():
return render_template("index.html")
@socketio.on("pty-input", namespace="/pty")
def pty_input(data):
if app.config["fd"]:
os.write(app.config["fd"], data["input"].encode())
@socketio.on("connect", namespace="/pty")
def connect():
(child_pid, fd) = pty.fork()
if child_pid == 0:
subprocess.run(app.config["cmd"])
else:
app.config["fd"] = fd
app.config["child_pid"] = child_pid
# ......
socketio.start_background_task(target=read_and_forward_pty_output)
print("task started")
def main():
# ......
socketio.run(app)
if __name__ == "__main__":
main()
命令是从Web端发送的,假设起始路径为/User/Example/App
,然后从Web端输入的顺序为:
some other commands
cd ..
some other commands
cd ..
some other commands
现在,当前路径应位于/User
,但如何使用代码获取此路径?
任何建议将不胜感激,谢谢:)