Python找不到我的值
我有一个python脚本从MySQL数据库中取出我的数据并将其转换为python字典。我试图使用in
关键字在python字典中找到我的会话令牌,但由于某种原因它不起作用。
我尝试使用这个python函数,看看是否可能有不同的字符串与一个奇怪的格式或字符在最后,但这不起作用:
函数:
def difference(string1, string2):
# Split both strings into list items
string1 = string1.split()
string2 = string2.split()
A = set(string1) # Store all string1 list items in set A
B = set(string2) # Store all string2 list items in set B
str_diff = A.symmetric_difference(B)
isEmpty = (len(str_diff) == 0)
if isEmpty:
print("No Difference. Both Strings Are Same")
else:
print("The Difference Between Two Strings: ")
print(str_diff)
print('The programs runs successfully.')
下面是代码。(这只是一个代码片段,加密密钥将在启动时更改)
try:
import Debug
Debug.difference(token, USERS['danielcaminero@plairstudio.com']['session'])
print(token)
print(USERS['danielcaminero@plairstudio.com']['session'])
print(USERS)
# check if user exists
if token not in USERS:
print("Doesnt work :(")
raise Exception('Invalid token')
except:
return {'message': 'Invalid token'}, 401
输出No Difference. Both Strings Are Same
The programs runs successfully.
2681bd95970590adcd5950a3f7c90ef7
2681bd95970590adcd5950a3f7c90ef7
{'danielcaminero@plairstudio.com': {'password': '44b4cedfad79d8e47d1430b4da4198f72162dbc0d49452d8dc968546e1b48f14', 'role': '1', 'id': 1, 'session': '2681bd95970590adcd5950a3f7c90ef7'}}
Doesnt work :(
127.0.0.1 - - [09/Apr/2023 14:00:52] "GET /protected HTTP/1.1" 401 -
我需要什么
请告诉我可能出了什么问题,或者我如何使这段代码与我的字典结构一起工作,因为我的程序的某些部分依赖于它。
这对你有用吗?
retval = None
for user in USERS:
if user['session'] == token:
print('found it!')
retval = user
break # no need to check all others
if not retval:
print('not found')
raise Exception('Invalid token')