Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5318503
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from flask import Flask, render_template, g, jsonify | ||
import sqlite3 | ||
import time | ||
|
||
DATABASE = 'push.sqlite' | ||
app = Flask(__name__) | ||
|
||
def connect_db(): | ||
return sqlite3.connect(DATABASE) | ||
|
||
def query_db(query, args=(), one=False): | ||
cur = g.db.execute(query, args) | ||
rv = [dict((cur.description[idx][0], value) | ||
for idx, value in enumerate(row)) for row in cur.fetchall()] | ||
return (rv[0] if rv else None) if one else rv | ||
|
||
@app.before_request | ||
def before_request(): | ||
g.db = connect_db() | ||
|
||
@app.teardown_request | ||
def teardown_request(exception): | ||
if hasattr(g, 'db'): | ||
g.db.close() | ||
|
||
@app.route('/') | ||
def hello_world(): | ||
return render_template('index.html') | ||
|
||
@app.route('/push/', methods=['POST']) | ||
def push(): | ||
while(1): | ||
push = query_db('select * from push where read = 0 limit 1'); | ||
if len(push) is not 0: | ||
g.db.execute('update push set read = 1 where id = %d' % push[0]['id']) | ||
g.db.commit() | ||
return jsonify(key=push[0]['key'], value=push[0]['value']) | ||
time.sleep(0.1) | ||
pass | ||
|
||
if __name__ == '__main__': | ||
app.debug = True | ||
app.run() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset='utf-8'> | ||
<meta http-equiv="X-UA-Compatible" content="chrome=1"> | ||
<title>python-push</title> | ||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> | ||
<script type="text/javascript"> | ||
$(document).ready(function(){ | ||
console.log('[document] ready'); | ||
|
||
initPush(); | ||
}); | ||
|
||
initPush = function() { | ||
console.log('[push] request fired, waiting'); | ||
$.post("/push/", { timestamp: (new Date().getTime()/1000) }, | ||
function(response) { | ||
console.log('[push] got push!'); | ||
|
||
switch(response.key) { | ||
case 'msg': | ||
$('#response').append('<h1>'+response.value+'</h1>'); | ||
break; | ||
} | ||
|
||
initPush(); | ||
}, 'json'); | ||
}; | ||
</script> | ||
|
||
</head> | ||
<body> | ||
<div id="response"></div> | ||
</body> | ||
</html> |