usr bin env python coding utf-8 import Bottle import sqlite3 import da

 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
#!/usr/bin/env python
# coding: utf-8
import Bottle
import sqlite3
import datetime
import string
import random
app = Bottle.Bottle()
def post_id_generator(size=10, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
@app.route('/')
def index():
connection = sqlite3.connect('bottle_db')
cursor = connection.cursor()
cursor.execute('SELECT * FROM main')
data = cursor.fetchall()
return Bottle.template('main.tpl', data=data)
@app.get('/add_msg')
def index():
return Bottle.template('add_post.tpl')
@app.post('/msg')
def add_msg():
post_id = post_id_generator()
post_header = Bottle.request.forms.get('post-header')
post_content = Bottle.request.forms.get('post-content')
date = datetime.date.today().strftime('%d:%m:%Y')
time = datetime.datetime.now().time().strftime('%H/%M/%S')
connection = sqlite3.connect('bottle_db')
cursor = connection.cursor()
data = (date, time, post_id, post_header, post_content)
cursor.execute('INSERT INTO main VALUES (?,?,?,?,?)', data)
connection.commit()
connection.close()
return post_header
@app.route('/post/<pid>')
def index(pid):
connection = sqlite3.connect('bottle_db')
cursor = connection.cursor()
cursor.execute('SELECT date, time, post_header, post_content FROM main where pid=?', [pid])
data = cursor.fetchall()
return str(data)
Bottle.run(app, host='localhost', port=8080)