Created
December 8, 2013 13:38
-
-
Save shinriyo/7857609 to your computer and use it in GitHub Desktop.
実際のスコアのコード
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
# -*- coding:utf-8 -*- | |
from bottle import route, run | |
import sys | |
import hashlib | |
from datetime import datetime | |
from pymongo import Connection | |
from pymongo.errors import ConnectionFailure | |
secret_key = 'mySecretKey' | |
@route('/score/add/:name/:score/:my_hash/') | |
def add_score(name='unknown', score='0', my_hash='unknown'): | |
print ("try") | |
try: | |
c = Connection(host="localhost", port=27017) | |
except(ConnectionFailure, e): | |
sys.stderr.write("Could not connect to MongoDB: %s" % e) | |
sys.exit(1) | |
dbh = c["mydb"] | |
real_hash = hashlib.md5(bytes(name + score + secret_key, "utf-8")).hexdigest() | |
assert dbh.connection == c | |
if real_hash == my_hash.lower(): | |
print ("hash is correct") | |
user_doc = { | |
"name" : name, | |
"score" : score | |
} | |
dbh.users.insert(user_doc, safe=True) | |
return ("Successfully inserted document: %s" % user_doc) | |
else: | |
print ("hash is not correct") | |
print ("my_hash is " + my_hash) | |
print ("real_hash is " + real_hash) | |
@route('/score/display/') | |
def display(): | |
try: | |
c = Connection(host="localhost", port=27017) | |
except(ConnectionFailure, e): | |
sys.stderr.write("Could not connect to MongoDB: %s" % e) | |
sys.exit(1) | |
dbh = c["mydb"] | |
assert dbh.connection == c | |
#users = dbh.users.find({"name":"shinriyo"}) | |
users = dbh.users.find() | |
result = '' | |
for user in users: | |
result += 'name: ' + str(user.get("name")) + ' score: ' + str(user.get("score")) + '\n' | |
return result | |
run(host='localhost', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment