python - 当详细信息肯定在数据库中时,验证电子邮件和散列密码总是返回 None

标签 python postgresql python-2.7 hash hashlib

我有一个简单的程序来检查电子邮件和散列密码是否在 Postgres 数据库中(它本来是一个 cgi 应用程序,但因为它失败了,我首先将它作为脚本进行测试):

import cgi
import cgitb
import hashlib
import psycopg2
from dbconfig import *
cgitb.enable()
print "Content-Type: text/plain\n\n";

def checkPass():
    email = "person@gmail.com"
    password = "mypassword"
    password = hashlib.sha224(password.encode()).hexdigest()
    result = cursor.execute('SELECT * FROM logins WHERE email=%s AND passhash=%s', (email, password))
    print result


if __name__ == "__main__":
    conn_string = "host=%s dbname=%s user=%s password=%s" % (host, database, dbuser, dbpassword)
    conn = psycopg2.connect(conn_string)
    cursor = conn.cursor()
    checkPass()

这个程序总是在打印时返回 None。电子邮件和散列密码肯定在数据库中。我把它们放在那里:

email = "person@gmail.com"
allpass = "mypassword"
password = hashlib.sha224(allpass.encode()).hexdigest()
cursor.execute('INSERT INTO logins (email, passhash) VALUES (%s, %s);', (email, password))
conn.commit()

检查数据库显示有电子邮件和散列密码。那么为什么第一个程序不在 SELECT 语句中返回一些东西呢?哈希不是以相同的方式存储的吗?任何帮助将不胜感激。

最佳答案

我弄清楚出了什么问题。碰巧的是,cursor.execute() 实际上并没有返回任何东西。你必须为此使用 fetchone()。

cursor.execute('SELECT * FROM logins WHERE email=%s AND passhash=%s;', (email, password))
result = cursor.fetchone()
print result

这会打印出正确结果的元组。

关于python - 当详细信息肯定在数据库中时,验证电子邮件和散列密码总是返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43476331/

相关文章:

python - Python -lxml xpath返回空列表

sql - 表函数中的 PostgreSQL 参数化 Order By/Limit

postgresql - Docker 如何向正在运行的容器添加卷?

python - Python 中的“正确”四舍五入到小数点后 3 位

python - 在类外部定义的方法,其本身随后被导入

python - 去除点和线等图像噪声

python - `key=function` 中的 `sorted(tuple, key=function)` 是如何工作的?

python - 来自谷歌的 400 错误请求

c# - Dapper.net "where ... in"查询不适用于 PostgreSQL

python - 无法使用 "PicklingError"在 Windows 10 上启动 Celery worker