Python如何實(shí)現(xiàn)遠(yuǎn)程方法調(diào)用
問題
你想在一個消息傳輸層如 sockets 、multiprocessing connections 或 ZeroMQ 的基礎(chǔ)之上實(shí)現(xiàn)一個簡單的遠(yuǎn)程過程調(diào)用(RPC)。
解決方案
將函數(shù)請求、參數(shù)和返回值使用pickle編碼后,在不同的解釋器直接傳送pickle字節(jié)字符串,可以很容易的實(shí)現(xiàn)RPC。 下面是一個簡單的PRC處理器,可以被整合到一個服務(wù)器中去:
# rpcserver.py
import pickle
class RPCHandler:
def __init__(self):
self._functions = { }
def register_function(self, func):
self._functions[func.__name__] = func
def handle_connection(self, connection):
try:
while True:
# Receive a message
func_name, args, kwargs = pickle.loads(connection.recv())
# Run the RPC and send a response
try:
r = self._functions[func_name](*args,**kwargs)
connection.send(pickle.dumps(r))
except Exception as e:
connection.send(pickle.dumps(e))
except EOFError:
pass
要使用這個處理器,你需要將它加入到一個消息服務(wù)器中。你有很多種選擇, 但是使用 multiprocessing 庫是最簡單的。下面是一個RPC服務(wù)器例子:
from multiprocessing.connection import Listener
from threading import Thread
def rpc_server(handler, address, authkey):
sock = Listener(address, authkey=authkey)
while True:
client = sock.accept()
t = Thread(target=handler.handle_connection, args=(client,))
t.daemon = True
t.start()
# Some remote functions
def add(x, y):
return x + y
def sub(x, y):
return x - y
# Register with a handler
handler = RPCHandler()
handler.register_function(add)
handler.register_function(sub)
# Run the server
rpc_server(handler, ('localhost', 17000), authkey=b'peekaboo')
為了從一個遠(yuǎn)程客戶端訪問服務(wù)器,你需要創(chuàng)建一個對應(yīng)的用來傳送請求的RPC代理類。例如
import pickle
class RPCProxy:
def __init__(self, connection):
self._connection = connection
def __getattr__(self, name):
def do_rpc(*args, **kwargs):
self._connection.send(pickle.dumps((name, args, kwargs)))
result = pickle.loads(self._connection.recv())
if isinstance(result, Exception):
raise result
return result
return do_rpc
要使用這個代理類,你需要將其包裝到一個服務(wù)器的連接上面,例如:
>>> from multiprocessing.connection import Client
>>> c = Client(('localhost', 17000), authkey=b'peekaboo')
>>> proxy = RPCProxy(c)
>>> proxy.add(2, 3)
5
>>> proxy.sub(2, 3)
-1
>>> proxy.sub([1, 2], 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rpcserver.py", line 37, in do_rpc
raise result
TypeError: unsupported operand type(s) for -: 'list' and 'int'
>>>
要注意的是很多消息層(比如 multiprocessing )已經(jīng)使用pickle序列化了數(shù)據(jù)。 如果是這樣的話,對 pickle.dumps() 和 pickle.loads() 的調(diào)用要去掉。
討論
RPCHandler 和 RPCProxy 的基本思路是很比較簡單的。 如果一個客戶端想要調(diào)用一個遠(yuǎn)程函數(shù),比如 foo(1, 2, z=3) ,代理類創(chuàng)建一個包含了函數(shù)名和參數(shù)的元組 ('foo', (1, 2), {'z': 3}) 。 這個元組被pickle序列化后通過網(wǎng)絡(luò)連接發(fā)生出去。 這一步在 RPCProxy 的 __getattr__() 方法返回的 do_rpc() 閉包中完成。 服務(wù)器接收后通過pickle反序列化消息,查找函數(shù)名看看是否已經(jīng)注冊過,然后執(zhí)行相應(yīng)的函數(shù)。 執(zhí)行結(jié)果(或異常)被pickle序列化后返回發(fā)送給客戶端。我們的實(shí)例需要依賴 multiprocessing 進(jìn)行通信。 不過,這種方式可以適用于其他任何消息系統(tǒng)。例如,如果你想在ZeroMQ之上實(shí)習(xí)RPC, 僅僅只需要將連接對象換成合適的ZeroMQ的socket對象即可。
由于底層需要依賴pickle,那么安全問題就需要考慮了 (因?yàn)橐粋€聰明的黑客可以創(chuàng)建特定的消息,能夠讓任意函數(shù)通過pickle反序列化后被執(zhí)行)。 因此你永遠(yuǎn)不要允許來自不信任或未認(rèn)證的客戶端的RPC。特別是你絕對不要允許來自Internet的任意機(jī)器的訪問, 這種只能在內(nèi)部被使用,位于防火墻后面并且不要對外暴露。
作為pickle的替代,你也許可以考慮使用JSON、XML或一些其他的編碼格式來序列化消息。 例如,本機(jī)實(shí)例可以很容易的改寫成JSON編碼方案。還需要將 pickle.loads() 和 pickle.dumps() 替換成 json.loads() 和 json.dumps() 即可:
# jsonrpcserver.py
import json
class RPCHandler:
def __init__(self):
self._functions = { }
def register_function(self, func):
self._functions[func.__name__] = func
def handle_connection(self, connection):
try:
while True:
# Receive a message
func_name, args, kwargs = json.loads(connection.recv())
# Run the RPC and send a response
try:
r = self._functions[func_name](*args,**kwargs)
connection.send(json.dumps(r))
except Exception as e:
connection.send(json.dumps(str(e)))
except EOFError:
pass
# jsonrpcclient.py
import json
class RPCProxy:
def __init__(self, connection):
self._connection = connection
def __getattr__(self, name):
def do_rpc(*args, **kwargs):
self._connection.send(json.dumps((name, args, kwargs)))
result = json.loads(self._connection.recv())
return result
return do_rpc
實(shí)現(xiàn)RPC的一個比較復(fù)雜的問題是如何去處理異常。至少,當(dāng)方法產(chǎn)生異常時服務(wù)器不應(yīng)該奔潰。 因此,返回給客戶端的異常所代表的含義就要好好設(shè)計(jì)了。 如果你使用pickle,異常對象實(shí)例在客戶端能被反序列化并拋出。如果你使用其他的協(xié)議,那得想想另外的方法了。 不過至少,你應(yīng)該在響應(yīng)中返回異常字符串。我們在JSON的例子中就是使用的這種方式。
以上就是Python如何實(shí)現(xiàn)遠(yuǎn)程方法調(diào)用的詳細(xì)內(nèi)容,更多關(guān)于Python遠(yuǎn)程方法調(diào)用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python如何利用plt.legend()添加圖例代碼示例
用python的matplotlib畫圖時,往往需要加圖例說明,下面這篇文章主要給大家介紹了關(guān)于python如何利用plt.legend()添加圖例的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
解決PyCharm 中寫 Turtle代碼沒提示以及標(biāo)黃的問題
這篇文章主要介紹了解決PyCharm 中寫 Turtle代碼沒提示以及標(biāo)黃的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
使用Python實(shí)現(xiàn)為PDF文檔設(shè)置和移除密碼
在數(shù)字化時代,文檔的安全性變得越來越重要,特別是對于包含敏感信息的PDF文件,所以本文主要來和大家介紹一下如何使用Python實(shí)現(xiàn)為PDF文檔設(shè)置和移除密碼,需要的可以參考下2024-03-03
最詳細(xì)的python工具Anaconda+Pycharm安裝教程
這篇文章主要介紹了最詳細(xì)的python工具Anaconda+Pycharm安裝教程,文中有非常詳細(xì)的圖文示例,對不會安裝的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04
Python3使用xlrd、xlwt處理Excel方法數(shù)據(jù)
這篇文章主要介紹了Python3使用xlrd、xlwt處理Excel方法數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
TensorFlow神經(jīng)網(wǎng)絡(luò)構(gòu)造線性回歸模型示例教程
這篇文章主要為大家介紹了TensorFlow構(gòu)造線性回歸模型示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11

