服务端代码:
from xmlrpc.server import SimpleXMLRPCServer
def speak_your_words(s):
print("message from client:", s)
return "message from server: " + s
server = SimpleXMLRPCServer(("localhost",9999)) # 绑定端口
server.register_function(speak_your_words) # 注册函数
server.serve_forever() #启动监听
编码完成后运行脚本,开始监听端口
客户端iPython中调用:
>>> from xmlrpc.client import ServerProxy
>>> s = ServerProxy("http://localhost:9999")
>>> s.speak_your_words("chris")
>>> 'message from server: hello chris'
服务端输出:
message from client: hello chris
127.0.0.1 - - [25/Sep/2018 15:47:04] "POST /RPC2 HTTP/1.1" 200 -