-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREST_server.py
executable file
·95 lines (83 loc) · 2.49 KB
/
REST_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python2
# Found on http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-rest-web-service-with-python.php
import web
import json
import psycopg2
import uuid
urls = (
'/db/(.*)', 'REST_db',
)
class REST_db:
sessions = {}
def GET(self, sub):
sub=sub.split('/')
i=web.input()
if sub[0] == 'session':
if sub[1] == 'open':
s = db_session(i)
self.sessions[s.ID] = s
ret = s.ID
elif sub[1] == 'close':
del self.sessions[i.sid]
ret = i.sid
else:
try:
s = self.sessions[i.gid]
methodToCall = getattr(s, sub[0])
ret=methodToCall(sub[1:], i)
except Exception as e:
print(e)
raise web.notfound()
return ""
web.header('Content-Type', 'application/json')
return json.dumps(ret)
def POST(self, sub):
web.header('Content-Type', 'application/json')
return json.dumps(tables)
class db_session():
def __init__(self, i):
self.cn = psycopg2.connect("host='localhost' dbname='{0}'".format(i.db))
self.transactions = []
self.ID = str(uuid.uuid4())
self.gid = i.gid
def __del__(self):
self.cn.rollback()
self.cn.close()
def tables(self, sub, i):
cur=self.cn.cursor()
tables=[]
cur.execute('select schemaname, tablename from pg_tables')
for r in cur:
t={}
t['schema'] = r[0]
t['table'] = r[1]
tables.append(t)
return tables
def transaction(self, sub, i):
if sub[0] == 'begin':
mySessionID = i.sid
mytransID = str(uuid.uuid4())
mySession = mySessions[i.session]
mySession[transactions] += mytransID
elif sub[0] == 'commit':
mytransID = i.tid
return mytransID
def query_execute(self, sub, i):
if sub[0] == 'execute':
ret={}
ret['sid'] = 3
ret['tid'] = 2
elif sub[0] == 'get':
cur=self.cn.cursor()
cur.execute('select * from '+table)
ret=[]
for r in cur:
ret.append(r)
elif sub[0] == 'num':
ret={}
ret['sid'] = 3
ret['tid'] = 2
return ret
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()