jsonrpc

相关网站:https://pypi.org/project/python-jsonrpc/

1.安装

pip install python-jsonrpc

2.服务端

#!/usr/bin/env python
# coding: utf-8

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 = ('45.77.10.62', 8080),
    RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()
View Code

3.客户端

#!/usr/bin/env python
# coding: utf-8

import pyjsonrpc

http_client = pyjsonrpc.HttpClient(
    url = "http://45.77.10.62:8080",
    username = "",
    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)
View Code

4.开防火墙

iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

 

5.现象

 

posted @ 2018-07-18 19:03  YoZane  阅读(295)  评论(0编辑  收藏  举报