在两个不同的python程序之间传递开放连接

时间:2019-04-05 03:06:40

标签: python

在两个不同的python程序之间传递打开的连接

这是我的用例。

我想连接到ssh服务器(网络/ linux / Windows)并打开连接。 并让另一个python程序继续使用第1步中的打开会话并提供用户输入(命令) 程序必须分开的原因是调用者将这些程序编排到设计器工作流中的图形处理流程中,例如mistral。

1 个答案:

答案 0 :(得分:2)

我可以通过任何东西吗? (我至少可以肯定)

...为什么不只是通过凭据并使其打开连接?

或者基本上您的第一个程序必须运行某种服务器,该服务器侦听命令并将其转发到树上……我想是这样的

在两个应用程序之间传递信息的一种方法可能是在一个烧瓶服务器上运行一个,而另一个服务器调用烧瓶端点(您不必使用flask ...有很多方法可以做到这一点)

import argparse
import requests
from flask import Flask,request

def prog_1():
    ''' manage some open connection '''
    my_open_thing = OpenConnection(stuff)
    app = flask.Flask("__main__")
    @app.route("/execute"):
    def execute_command():
        if request.form.get("CMD",None):
           my_open_thing.send(request.form['CMD'])
           return my_open_thing.recv().to_string()
    app.run(port=23123)

def prog_2():
    '''interact with other thing'''
    while 1:
      cmd = input("CMD:")
      if cmd in ["quit","q"]:
         break
      print(requests.post("http://localhost:23123/execute",{"CMD":cmd}).content)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('TYPE',choices=['manager','client'],help="Serve the connection, or use the manager")
    parser.parse_args()
    if parser.TYPE == "manager":
        prog_1()
    else:
        prog_2()