使用 python 在服务器之间创建 API
Creating a API between servers with python
我想制作一个在两台服务器上运行的 python 脚本 我想通过在服务器之间发送信息的方式来中断服务器之间的工作。创建一个 API 是建立这个任务的最佳方式吗?还有其他方法可以实现吗?
这是制作线程服务器的简单方法,客户端可以在其中连接、断开连接和发送信息。希望这对您有所帮助。
import threading
class Server:
def __init__(self, name):
self.name = name
self.clients = []
self.messages = []
self.lock = threading.Lock()
def register(self, client):
with self.lock:
self.clients.append(client)
def unregister(self, client):
with self.lock:
self.clients.remove(client)
def send(self, msg):
with self.lock:
self.messages.append(msg)
def get_messages(self):
with self.lock:
messages = self.messages[:]
self.messages = []
return messages
def get_clients(self):
with self.lock:
return self.clients[:]
class Client:
def __init__(self, name):
self.name = name
self.server = None
def connect(self, server):
self.server = server
self.server.register(self)
def disconnect(self):
self.server.unregister(self)
self.server = None
def send(self, msg):
self.server.send(msg)
def get_messages(self):
return self.server.get_messages()
def get_clients(self):
return self.server.get_clients()
Server1 = Server("Server1")
Client1 = Client("Client1")
Client2 = Client("Client2")
#connect the clients to the servers
Client1.connect(Server1)
Client2.connect(Server1)
#get the clients from the servers
print(Server1.get_clients())
#send messages from the clients
Client1.send("Hello")
Client2.send("Hi")
#get the messages from the servers
print(Server1.get_messages())
#disconnect the clients from the servers
Client1.disconnect()
Client2.disconnect()
我想制作一个在两台服务器上运行的 python 脚本 我想通过在服务器之间发送信息的方式来中断服务器之间的工作。创建一个 API 是建立这个任务的最佳方式吗?还有其他方法可以实现吗?
这是制作线程服务器的简单方法,客户端可以在其中连接、断开连接和发送信息。希望这对您有所帮助。
import threading
class Server:
def __init__(self, name):
self.name = name
self.clients = []
self.messages = []
self.lock = threading.Lock()
def register(self, client):
with self.lock:
self.clients.append(client)
def unregister(self, client):
with self.lock:
self.clients.remove(client)
def send(self, msg):
with self.lock:
self.messages.append(msg)
def get_messages(self):
with self.lock:
messages = self.messages[:]
self.messages = []
return messages
def get_clients(self):
with self.lock:
return self.clients[:]
class Client:
def __init__(self, name):
self.name = name
self.server = None
def connect(self, server):
self.server = server
self.server.register(self)
def disconnect(self):
self.server.unregister(self)
self.server = None
def send(self, msg):
self.server.send(msg)
def get_messages(self):
return self.server.get_messages()
def get_clients(self):
return self.server.get_clients()
Server1 = Server("Server1")
Client1 = Client("Client1")
Client2 = Client("Client2")
#connect the clients to the servers
Client1.connect(Server1)
Client2.connect(Server1)
#get the clients from the servers
print(Server1.get_clients())
#send messages from the clients
Client1.send("Hello")
Client2.send("Hi")
#get the messages from the servers
print(Server1.get_messages())
#disconnect the clients from the servers
Client1.disconnect()
Client2.disconnect()