【问题标题】:Is there a way to send (python) code to a server to compile and run but display the results on the computer that sent them?有没有办法将(python)代码发送到服务器进行编译和运行,但在发送它们的计算机上显示结果?
【发布时间】:2016-10-07 19:44:07
【问题描述】:

我刚买了一台服务器,想知道是否有办法远程运行代码但在本地存储/显示结果。例如,我编写了一些代码来显示图形,图形上的位置由(远程)服务器计算,但图形显示在(本地)平板电脑上。

我想这样做是因为我每天随身携带的平板电脑对于计算物理模拟来说非常慢。我知道我可以设置某种通信协议,允许服务器计算事物,然后将计算发送到我的平板电脑,以便在我的平板电脑上使用脚本来处理数据。但是,我希望避免在每次运行新模拟时都编写一组新的通信脚本(以处理不同格式的数据)。

【问题讨论】:

    标签: python remote-access


    【解决方案1】:

    这是一个完整的“The Russians used a pencil”解决方案,但您是否考虑过在进行计算的机器上运行 VNC 服务器?

    您可以在平板电脑/手机/PC 上安装 VNC 客户端并以这种方式查看,有很多可用的。无需从头开始创建任何东西。

    【讨论】:

      【解决方案2】:

      使用 ssh,您可以使用 python 脚本或 shell 脚本来执行此操作。

      ssh machine_name "python" < ~/script/path/script.py

      正如 OP 在 cmets 中表示他想与远程机器上的脚本进行交互,我在这里做了一些更改。

      1. 将 python 或 shell 脚本复制到远程计算机。这可以通过多种方式完成。例如scp。而且,ssh,就像这里:

      ssh machine_name bash -c "cat > /tmp/script.py" < ~/script/path/script.py

      1. 与远程机器上的脚本交互

      ssh machine_name python -u /tmp/script.py

      您现在应该可以与在远程机器上运行的脚本进行交互了!

      注意使用-u 将python 的stdin/stdout 设置为无缓冲模式。这是能够与脚本交互所必需的。

         -u     Force stdin, stdout and stderr to be totally unbuffered.  On systems where it  matters,  also  put  stdin,
                stdout  and stderr in binary mode.  Note that there is internal buffering in xreadlines(), readlines() and
                file-object iterators ("for line in sys.stdin") which is not influenced by this option.   To  work  around
                this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.
      

      这是一个例子。 复制到服务器的代码:

      #!/usr//bin/env python3
      
      while True:
          value = input("Please enter the value: ")
          if value != "bye":
              print("Input received from the user is: ", value)
          else:
              print("Good bye!!")
              break
      

      互动环节:

      $ ssh  machine_name python -u python/pyecho.py
      Please enter the value: 123
      Input received from the user is:  123
      Please enter the value: bye 
      Good bye!!
      

      参考:

      【讨论】:

      • 只要script.py 本身不尝试从标准输入读取数据,它就可以工作。
      • @chepner 该解决方案自 2020 年编辑以来运行良好。它可以毫无问题地从标准输入读取和写入。
      • 其他人投了反对票,而不是我。 (顺便说一句,scp ~/script/path/script.py desktop:/tmp/script.py 要简单得多。)更新仍然假定脚本将从标准输入读取,而不是直接从键盘读取(但这是一种改进)。
      • 不,脚本可以在键盘上正常工作。
      • 这取决于脚本,而不是您连接标准输入的方法。无论如何,cat - | ssh ... 只是写ssh ... 的一种复杂方式。 cat 不带任何参数从本地标准输入读取(- 用于将标准输出与 其他 文件连接起来),ssh 从本地标准输出读取不带管道。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 2023-03-31
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多