簡體   English   中英

如何通過套接字python發送函數

[英]How to send a function over sockets python

所以我正在學習套接字,特別是客戶端服務器網絡,我有一個問題是是否可以通過套接字發送函數?

我知道你可以做這樣的事情:

#class containing function, called printing_class
print.py
class printing_class(self):

def print(self):
    print("hello world")
#server.py
from print import printing_class

print = printing_class(self)
server.send(print) #pseudocode for send statement, sending print object

#client.py
print = client.receive() #pseudo code for print and receive
print.print() # by doing this, prints out hello world

通過發送一個對象,我可以訪問它的方法然后打印它。

但是,我想知道有沒有辦法做到這一點:

#server.py
server.send(print("hello world"))

#client.py
client.receive() # when receiving from server, automatically prints it

是否可以通過套接字發送打印功能,當客戶端收到它時,它會自動打印它?

可以使用名為RPyC的庫來實現。

  1. 為簡單的服務器運行包含的rpyc_classic.py腳本(僅用於測試)。
  2. 使用客戶端連接到它並發送您想要執行的 python 代碼。

例子:

import rpyc
c = rpyc.classic.connect("localhost") # connect
c.execute("print('hi there')")   # print "hi there" on the host

rpyc 的眾多功能之一是您可以限制要執行的功能(或者您可以自己定義它們),以免造成安全漏洞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM