无法在 Python 3.x 中登录

Posted

技术标签:

【中文标题】无法在 Python 3.x 中登录【英文标题】:Can't make log in in Python 3.x 【发布时间】:2018-07-18 03:55:56 【问题描述】:

我已经创建了一个数据库,需要检查那个数据库是否有相应的用户名和密码……如果有,它应该打印“成功”,否则不会……

我尝试了很多方法,只有在用户名和密码正确的情况下才会打印...否则它什么也不显示。

它没有显示无法登录或错误。它只以 0 错误退出,没有任何显示。

#!/usr/bin/python

import pymysql

# Open database connection
db = pymysql.connect("localhost","root","","text" )

# prepare a cursor object using cursor() method
cursor = db.cursor()
eg = input("What's your user name? ")
password = input("What's your user password? ")

sql = ("SELECT * FROM login WHERE name = '%s' AND password= '%s' " % (eg,password))

try:
    # Execute the SQL command
    cursor.execute(sql)
    results = None
    # Fetch all the rows in a list of lists.
    results = cursor.fetchall()
    try:
        if results is not None:
            for row in results:
                id = row[0]
                name = row[1]
                password = row[2]
                print("login success")
    except:
        print("Error you arenot in the planet dude sign up first")

except:
    print("Error you arenot in the planet dude sign up first")

# disconnect from server
db.close()

【问题讨论】:

您不应该将密码保存在数据库中,先散列然后使用像 passlib 这样的 skating。 bitbucket.org/ecollins/passlib/wiki/Home 【参考方案1】:

缩进似乎有些问题,请尝试以下部分:

try:
    # Execute the SQL command
    cursor.execute(sql)
    results = None
    # Fetch all the rows in a list of lists.
    results = cursor.fetchall()
    try:
        if results is not None :
            for row in results:
                id = row[0]
                name = row[1]
                password= row[2]
                print ("login success")
    except:
    print("Error you arenot in the planet dude sign up first")

except:
    print ("Error you are not in the planet dude sign up first")

【讨论】:

【参考方案2】:
for row in results:

   id = row[0]
   name = row[1]
   password= row[2]
   print ("login success")

应该是

success = False
for row in results:

   db_id = row[0]
   db_name = row[1]
   db_password= row[2]

   if db_name == eg and db_password == password:
       success = True
       print ("login success")

row[1]row[2] 创建的变量需要不同于上面创建的eqpassword 变量。完成后,您只需比较它们以查看它们是否匹配。

您可以稍后检查成功变量以仅在登录过程成功或不成功时执行操作。

【讨论】:

按你说的那样尝试 CDspace ,,,但是如果我们输入正确的用户名和密码,它就会移动到 except 尝试删除 try except 子句,这样您就可以看到遇到了什么错误。 我打错了。它应该是例如不是 eq。 但我有疑问........,,当“如果”=>“如果成功== False:打印(“错误:先注册”)时,我得到了正确答案” 在“for”之外..当我放在“for”中时,我没有给出答案。从逻辑上讲,如果它在for循环内或for循环外,成功将是错误的,所以当我放入“for”时它应该打印错误,但是当我放入for循环时它不会给出错误跨度>

以上是关于无法在 Python 3.x 中登录的主要内容,如果未能解决你的问题,请参考以下文章

无法在python 3.x中安装wsgiref。

Python 3.x 无法将 Decimal() 序列化为 JSON

Python 请求无法登录

使用python的putty登录时无法设置标题。

用于登录 Facebook 的 Python 社交身份验证无法在生产服务器上运行

无法使用 Python 登录亚马逊