如何使用 python tcp 套接字 运行 交互命令?

How to run interactive commands using python tcp socket?

假设我想 运行 vim ./foo.txt 我希望能够使用我的 tcp 套接字客户端编辑 foo.txt。

但每当我尝试这样做时,它都会在服务器上执行,但不会在我的客户端上执行。

我不想使用 paramiko 或任何其他类似 ssh 的模块我想继续使用 python 套接字模块。

我正在使用 python 3.

我建议使用子进程模块在服务器(文件所在的位置)上打开一个命令。这样可以不断地将信息放入其中。您可以让客户端发送一条消息,告诉服务器将 x 发送到子进程。

一个例子是这样的:

import subprocess

# Assuming the variable x is that the socket is sending the server...
editing_foo = subprocess.Popen(['vim', './foo.txt'], stdin=PIPE)  # stdin must be PIPE to communicate
editing_foo.communicate(input=x)  # input is a string which is sent to the subprocess
# x could have been 'i' or ':q!' for example