本章内容:
1、Flask-restful
2、ansible
3、jsonrpc api
4、zabbix api
5、token
1、Flask-restful
#!/usr/bin/env python #coding:utf8 from flask import Flask,request from flask.ext.restful import Resource,Api,reqparse,abort import json app = Flask(__name__) api = Api(app) todos = { '1' : {'task':'build an API'}, '2' : {'task':'build a web'}, '3' : {'task':'build a app' } } # 资源不存在时的返回 def abort_if_todo_doesnt_exist(todo_id): if todo_id not in todos: abort(404,message="todo {} doesn't exist".format(todo_id)) # 获取传入的变量task值 parser = reqparse.RequestParser() parser.add_argument('task',type=str) # 资源的查、改、删,需要传入id class Todo(Resource): def get(self,todo_id): abort_if_todo_doesnt_exist(todo_id) return {todo_id:todos[todo_id]} def put(self,todo_id): abort_if_todo_doesnt_exist(todo_id) args = parser.parse_args() print args task = {'task':args['task']} # request.args.get('task') todos[todo_id] = task return {todo_id:todos[todo_id]},201 def delete(self,todo_id): abort_if_todo_doesnt_exist(todo_id) del todos[todo_id] return '',204 # 资源的查所有 添加,不需要传入id class TodoList(Resource): def get(self): return todos def post(self): args = parser.parse_args() todo_id = str(int(max(todos))+1) todos[todo_id] = {'task':args['task']} return todos[todo_id],201 # 定义两个场景的访问路由 api.add_resource(Todo,'/<string:todo_id>') api.add_resource(TodoList,'/') if __name__ == '__main__': app.run(host='0.0.0.0',port=7777,debug=True)
2、ansible
3、jsonrpc api
#服务端 #!/usr/bin/env python #coding:utf-8 from flask import Flask,render_template,request from flask_jsonrpc import JSONRPC import json app = Flask(__name__) jsonrpc = JSONRPC(app, '/api') @jsonrpc.method('user.create') #响应有不定参数传入的method,最常用 def user(**kwargs): data = {} data['name'] = kwargs.get('name',None) data['age'] = kwargs.get('age',None) return 'I am %s,age is %s' % (data['name'],data['age']) if __name__=='__main__': app.debug=True app.run(host='0.0.0.0',port=5001)
#客户端 #!/bin/env python # -*- encoding: utf-8 -*- from __future__ import unicode_literals import json import requests headers = {'content-type': 'application/json'} url = "http://127.0.0.1:5001/api" data = { 'jsonrpc':'2.0', 'method': 'user.create', #请求后端不定参数的method 'id':'1', 'params':{ 'name':'wd', #无参数的method,此处为空,指定参数的method,只能保留一条参数 'age':'18' } } r = requests.post(url, headers=headers,json=data) print r.status_code print r.text
4、zabbix api
zabbix登录接口是咧 import requests import json headers = {'content-type': 'application/json'} url = "http://123.59.185.96/zabbix/api_jsonrpc.php" def zabbix_login(): data = { 'jsonrpc':'2.0', 'method': 'user.login', 'id':'1', 'params':{ 'user':'admin', 'password':'zabbix' } } r = requests.post(url, headers=headers,json=data) return r.text def tokens(): token = json.loads(zabbix_login())['result'] return token def hostlist(): data = { 'jsonrpc':'2.0', 'method':'host.get', "params": { "output": "extend", "filter": { "host": [ "Zabbix server", "Linux server" ] } }, "auth": tokens(), "id": 1 } r = requests.post(url, headers=headers,json=data) return r.text print hostlist()
5、token