python应用程序之间的通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37971151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:10:57  来源:igfitidea点击:

Communication between python applications

pythonmultithreadingserverclient

提问by Lufftre

I'm having trouble finding a good solution for a client server system where the clients make requests to the server and the server responds with the result. The server needs to be independent from the clients, i.e. if I create a new python application I want to easily be able to connect to the server and send requests. One possible solution I know of is using sockets, but that feels unnecessary complicated. I've tried looking into using Managers but I'm not sure if it's on the right track.

我在为客户端服务器系统找到一个好的解决方案时遇到了麻烦,其中客户端向服务器发出请求,服务器以结果进行响应。服务器需要独立于客户端,即如果我创建一个新的 python 应用程序,我希望能够轻松连接到服务器并发送请求。我知道的一种可能的解决方案是使用套接字,但这感觉不必要的复杂。我试过考虑使用 Managers,但我不确定它是否在正确的轨道上。

My goal is to have something working like this:

我的目标是让事情像这样工作:

result = server.send("2+2") # result should now be 4

回答by systemHyman

Check out ZeroMQ. It can be used point to point like a super powered socket and looks nice and clean in your code like your example. Thisexampleshows how easy it can be.

退房ZeroMQ。它可以像超级电源插座一样点对点使用,并且在您的代码中看起来像您的示例一样干净整洁。这个例子展示了它是多么容易。

Or you could do a simple RESTinterface using a light weight web service framework like Bottleor Flaskon the server side. Those can run stand alone at first for ease of use and later put them behind a web server (nginxwith uwsgifor example) later for performance. On the client side talk to it with regular python requests module.

或者,您可以REST使用轻量级 Web 服务框架(如服务器端BottleFlask服务器端)来创建一个简单的界面。那些能够运行得,易于使用的独立,后来把他们后面的Web服务器(nginx带有uwsgi为例)后的性能。在客户端使用常规的 python requests 模块与它交谈。

回答by CristiFati

Once I played with these things (in Python) and here's an example (don't mind the code style, format, efficiency, as they are lame, lots of NO-NOs from programming's PoV, just look at it as it was a starting point):

一旦我玩过这些东西(在 Python 中),这是一个例子(不要介意代码风格、格式、效率,因为它们很蹩脚,编程的 PoV 中有很多 NO-NO,只是看看它,因为它是一个开始观点):

server.py:

服务器.py:

import sys
import select
from SocketServer import TCPServer, StreamRequestHandler

HOST = "127.0.0.1"
PORT = 12345

class DummyRequestHandler(StreamRequestHandler):
    def handle(self):
        text = ""
        while 1:
            if select.select([self.rfile], [], [], 0.5)[0] == [self.rfile]:
                char = self.rfile.read(1)
                if char == ";":
                    break
                else:
                    text += char

        try:
            result = eval(text)
        except Exception as e:
            result = str(e)
        self.wfile.write(str(result) + ";")

print "Listening on %s:%d" % (HOST, PORT)
server = TCPServer((HOST, PORT), DummyRequestHandler)
server.serve_forever()

client.py:

客户端.py:

import sys
import socket
import select

HOST = "127.0.0.1"
PORT = 12345
EXIT_TEXT = "EXIT"

text = ""
while not text:
    text = raw_input("Enter text to send (%s to exit) :" % EXIT_TEXT)
    if text.upper() == EXIT_TEXT:
        print "Exiting"
        sys.exit(0)
if text[-1] != ";":
    text += ";"
s4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s4.connect((HOST, PORT))
s4.send(text)
reply = ""
while 1:
    if select.select([s4], [], [], 0.5)[0] == [s4]:
        char = s4.recv(1)
        if char == ";":
            break
        else:
            reply += char
print "Server replied: %s\n" % reply
s4.close()

I've tested it using Python2.7.10on Win. It works with simple expressions.

我已经在Win上使用Python2.7.10对其进行了测试。它适用于简单的表达式。