-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
56 lines (38 loc) · 883 Bytes
/
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
import web
import sqlite3
urls = (
"/messages", "Message",
"/", "UIRedirect"
)
class Message:
def GET(self):
conn = sqlite3.connect("chat.db")
c = conn.cursor()
ret = "["
for row in reversed(list(c.execute("SELECT * FROM chat"))):
print row
ret+="\""+row[0]+"\","
ret +="\"nope\"]"
conn.close()
return ret
def POST(self):
conn = sqlite3.connect("chat.db")
c = conn.cursor()
message = web.input()["message"]
print message
c.execute("INSERT INTO chat VALUES(?)", (message,))
conn.commit()
conn.close()
class UIRedirect:
def GET(self):
return web.redirect("/static/ui.html")
app = web.application(urls, globals())
if __name__ == "__main__":
try:
conn = sqlite3.connect("chat.db")
c = conn.cursor()
c.execute("CREATE TABLE chat (message text)")
conn.close()
except sqlite3.OperationalError:
pass
app.run()