@marataziat
Джангист-тракторист

Что с кодом этим?

Я написал не большой код на Python для регистрации но когда человек приходит на главную страницу должен появится cookie_hash чего не происходит :((
import random
import hashlib
import sqlite3
from bottle import route, run, template, response, request
#configs and values
conn = sqlite3.connect('BlogsData.db')
c = conn.cursor()

# hashing password: hashlib.sha224("mypass").hexdigest()
# generate cookie hash: random.randrange(10000000000)
# For get and set cookie in bootle using:
# response.set_header('Set-Cookie', 'name=value')
# response.add_header('Set-Cookie', 'name2=value2')
# sqlte3 docs.python.org/2/library/sqlite3.html

# Save (commit) the changes
# conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
# conn.close()
# c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
@route('/reg/<name>/<password>')
def index(name, password):
	cookie_hash = random.randrange(10000000000)
	response.set_header('Set-Cookie', 'cookie_hash=%s' % (cookie_hash))
	c.execute("INSERT INTO UserData (login, password, cookie_hash) VALUES ('%s','%s', '%s')" % (name, password, cookie_hash))
	
	conn.commit()
	conn.close()
@route('/get')
def cookie():
	print request.get_cookie("cookie_hash")
	return request.get_cookie("cookie_hash")

@route('/hello')
def hello_again():
    if request.get_cookie("visited"):
        return "Welcome back! Nice to see you again"
    else:
        response.set_cookie("visited", "yes")
        return request.get_cookie("visited")

run(host='localhost', port=8100)
  • Вопрос задан
  • 190 просмотров
Решения вопроса 1
lxsmkv
@lxsmkv
Test automation engineer
from bottle import route, run, template, response
import random


@route('/reg/<name>/<pw>')
def index(name, pw):
  cookie_hash = random.randrange(10000000000)
  response.set_header('Set-Cookie', 'cookie_hash=%s' % (cookie_hash))
  return template('<b>Hello {{name}}, {{hash}}, {{pw}} </b>!', name=name, hash=cookie_hash, pw=pw)
run(host='localhost', port=8080)

у меня этот код работает, даю запрос на localhost:8080/reg/bottle/neck
смотрю в хедер ответа в файрфоксе - значение пишется в cookie_hash, именно то, которое было сгенерировано. Все пучком.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы