pyjsonrpc模块使用

  pyjsonrpc模块的远程过程调用方法。

# -*- coding:utf-8 -*- 
#!/usr/bin/env python2.7
# @Author  : tianbao
# @Contact : [email protected]
# @Time    : 2018/7/4 21:49
# @File    : aactest.py
# @Software: PyCharm
import pyjsonrpc

http_client = pyjsonrpc.HttpClient(
    url = "http://example.com/jsonrpc",
    username = "Username",
    password = "Password"
)
# 第一种调用方法
print http_client.call("add", 1, 2)
# Result: 3

# 第二种调用方法
# It is also possible to use the *method* name as *attribute* name.
print http_client.add(1, 2)
# Result: 3

# 没有返回值
# Notifications send messages to the server, without response.
http_client.notify("add", 3, 4)
client
# -*- coding:utf-8 -*- 
#!/usr/bin/env python2.7
# @Author  : tianbao
# @Contact : [email protected]
# @Time    : 2018/7/4 21:49
# @File    : aastest.py
# @Software: PyCharm
import pyjsonrpc


class RequestHandler(pyjsonrpc.HttpRequestHandler):

    @pyjsonrpc.rpcmethod
    def add(self, a, b):
        """Test method"""
        return a + b


# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
    server_address = ('localhost', 8080),  # 监听地址
    RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()                 # 启动
server

猜你喜欢

转载自www.cnblogs.com/guotianbao/p/9265703.html
今日推荐