| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #!/usr/bin/env python3
- from bottle import route, run, request, install, response
- from bottle_sqlite import SQLitePlugin
- import json
- install(SQLitePlugin(dbfile='test.db'))
- def turn_me_to_json(cursor,res):
- out = []
- for row in res:
- v = {}
- for i, val in enumerate(row):
- v[cursor.description[i][0]]=val
- out.append(v)
- return out
- @route('/machines/', method='GET')
- def get_machines(db):
- c = db.execute('SELECT mgroup,name,ip FROM machines')
- out = turn_me_to_json(c,c.fetchall())
- response.content_type = "application/json"
- return json.dumps(out)
- @route('/machines/<group>/<name>/<ip>', method='PUT')
- def put_machines(db, group, name, ip):
- c = db.execute('UPDATE machines set mgroup=? and name=? where ip=?', (group,name,ip))
- if c.rowcount == 0:
- c = db.execute('INSERT INTO machines (mgroup,name,ip) values(?,?,?)', (group,name,ip))
- @route('/domain/', method='GET')
- def get_domains(db):
- c = db.execute('SELECT ip,port,domain,subdomain FROM domains')
- out = turn_me_to_json(c,c.fetchall())
- response.content_type = "application/json"
- return json.dumps(out)
- @route('/domain/<ip>', method='PUT')
- def put_domain(db, ip="Mystery Domain" ):
- if request.json is None or "domain" not in request.json or "subdomain" not in request.json or "port" not in request.json:
- response.status=400
- return
- c = db.execute('UPDATE domains set domain=? and subdomain=? and port=? where ip=?',
- (request.json["domain"],request.json["subdomain"],request.json["port"],ip))
- if c.rowcount == 0:
- c = db.execute('INSERT INTO user_machines (ip,name) values(?,?)', (request.json["ip"],name))
- return
- run(host='localhost', port=8080)
|