为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过套接字发送函数 python

如何通过套接字发送函数 python

德玛西亚99 2023-12-12 15:05:45
所以我正在学习套接字,特别是客户端服务器网络,我的一个问题是是否可以通过套接字发送函数?我知道你可以这样做:#class containing function, called printing_classprint.pyclass printing_class(self):def print(self):    print("hello world")#server.pyfrom print import printing_classprint = printing_class(self)server.send(print) #pseudocode for send statement, sending print object#client.pyprint = client.receive() #pseudo code for print and receiveprint.print() # by doing this, prints out hello world通过发送一个对象,我可以访问它的方法然后打印它。但是,我想知道是否有办法做到这一点:#server.pyserver.send(print("hello world"))#client.pyclient.receive() # when receiving from server, automatically prints it是否可以通过套接字发送打印函数,当客户端收到它时,它会自动打印它?
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

它可以使用名为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 的众多功能之一是您可以限制要执行的函数(或者您可以自己定义它们),以免导致安全漏洞。


查看完整回答
反对 回复 2023-12-12
  • 1 回答
  • 0 关注
  • 41 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信