如何通过套接字发送函数 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

是否可以通过套接字发送打印函数,当客户端收到它时,它会自动打印它?


德玛西亚99
浏览 46回答 1
1回答

摇曳的蔷薇

它可以使用名为RPyC 的库来实现。rpyc_classic.py为简单服务器运行包含的脚本(仅用于测试)。使用客户端连接到它并发送您想要执行的 python 代码。例子:import rpyc c = rpyc.classic.connect("localhost") # connect c.execute("print('hi there')")   # print "hi there" on the hostrpyc 的众多功能之一是您可以限制要执行的函数(或者您可以自己定义它们),以免导致安全漏洞。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python