本文介绍了为什么我的两个python字符串在我的程序中不相同,但在解释器中是相同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用python编写聊天服务器。我使用SHA1哈希来验证用户,并将用户存储的哈希与给定密码的哈希进行比较,如果它们相同,那么我应该验证用户。



我的哈希函数如下所示:

  def sha1_encode(string):
import hashlib
return hashlib .sha1(bytes(string)).hexdigest()

我的验证用户如下所示:

  def validate_user(self,user,password):
如果self.users中的用户:
print用户存在
#获取保存的SHA1哈希,并查看它是否与给定
#password
printsha,sha1_encode(password)
printstored ,self.users [user]
printequal,self.users [user] == sha1_encode(password)
打印类型(self.users [user])
打印类型(sha1_encode (密码))
如果str(self.users [user])== str(sha1_encode(password)):
printvalidate loop entered
return True
else:
return False



  user exists $ b 

$ b sha 61503cfe0803f3a3b964b46a405f7828fd72b1f7
stored 61503cfe0803f3a3b964b46a405f7828fd72b1f7

equal False
< type'str'>
< type'str'>

所以我知道它们都是字符串,我知道它们是相同的,但对于一些原因返回错误。我原本是在质疑不同类型的物体,但似乎并非如此。



然后我试着将这些字符串复制到解释器中,并检查它们是否相等:

<$ p $在[3]中:x ='61503cfe0803f3a3b964b46a405f7828fd72b1f7'

== y
Out [3]:True

此时我很困惑为什么它不会在函数中报告真实并在解释器中报告真实,尤其是因为我似乎正在使用不同的变量名称做同样的事情。任何人都可以向我解释发生了什么事?任何帮助将不胜感激。

解决方案

在这里只需要一个刺,但根据您的输出,它看起来像可能会有一个尾随'\ n'您的存储的密码列表,因此输出后的空行

  printstored,self.users [user] 

您可以尝试

  printequal,self.users [user] .strip()== sha1_encode(password)



看看是否照顾你的问题。该呼叫将删除尾随字符。

I'm trying to write a chat server using python. I am using SHA1 hash to validate users and comparing the stored hash for the user to the hash of the given password and if they are the same then I should validate the user.

My hash function looks like this:

def sha1_encode(string):
    import hashlib
    return hashlib.sha1(bytes(string)).hexdigest()

and my validate user looks like this:

def validate_user(self, user, password):
    if user in self.users:
        print "user exists"
        #Get the saved SHA1 hash and see if it matches the hash of the given
        #password
        print "sha", sha1_encode(password)
        print "stored", self.users[user]
        print "equal", self.users[user] == sha1_encode(password)
        print type(self.users[user])
        print type(sha1_encode(password))
        if str(self.users[user]) == str(sha1_encode(password)):
            print "validate loop entered"
            return True
    else:
        return False

when I run this with a user I know is in the list, I get this output:

user exists
sha 61503cfe0803f3a3b964b46a405f7828fd72b1f7
stored 61503cfe0803f3a3b964b46a405f7828fd72b1f7

equal False
<type 'str'>
<type 'str'>

so I know both of them are strings and I know that they are both the same thing but for some reason return false. I originally was questioning the objects being of different types but that doesn't seem to be the case.

So then I tried to copy these strings into the interpreter and check if they were actually equal:

In [1]: x = '61503cfe0803f3a3b964b46a405f7828fd72b1f7'

In [2]: y = '61503cfe0803f3a3b964b46a405f7828fd72b1f7'

In [3]: x == y 
Out[3]: True

And at this point I'm confused why it's not reporting true in the function and reporting true in the interpreter, especially cause it seems like I am doing the same exact thing just with different variable names. Could anyone explain to me whats going on? Any help would be greatly appreciated.

解决方案

Just taking a stab here, but based on your output it looks like there might be a trailing '\n' in your stored password list thus the blank line in the output after

print "stored", self.users[user]

You could try

print "equal", self.users[user].strip() == sha1_encode(password)

to see if that takes care of your problem. The call will remove trailing characters.

这篇关于为什么我的两个python字符串在我的程序中不相同,但在解释器中是相同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:36