我有一个python服务器,当我问客户端登录名以检查它与数据库中的1是否有效时,由于某种原因,字符串的末尾总是有一个空格。我已经尝试过了:
newstring = oldstring.replace(' ', '') #not working, white space is still there
newstring = oldstring.strip() # also not working, whitespace is still there
#i also tried:
newstring = oldstring.replace('\n\t\r\s\f', '') #not working, white space is still there
这是服务器上的代码段:
client_loggedin = False
while client_loggedin == False:
accountName = conn.recv(1024).decode("utf_8")
print(accountName + " = " + str(len(accountName)))
db = pymysql.connect("XXXXXXXXXXXXXsecretXXXXXXXXXX")
cursor = db.cursor()
cursor.execute("SELECT `XXXXXXXXXXXXX` FROM `XXXXXXXXXXXXX` WHERE `XXXXXXXXXXXXX`=%s", accountName)
dataraw = cursor.fetchone()
data = dataraw[0]
db.close()
if str(accountName) != str(data):
print (str(data) + "+" + str(accountName))
print("datalengte: " + str(len(data)) + " en " + "accountNamelengte: " + str(len(accountName)))
print ("Deze gebruiker bestaat niet")
elif accountName == data:
reply = "Welcome " + str(accountName)
conn.sendall(reply.encode("utf_8"))
client_loggedin == True
for x in clientList:
if x != conn:
x.sendall((str(accountName) + " has joined the server").encode("utf_8"))
比方说,数据库中有一个帐户名称Oliver,而客户端发送Oliver。由于某些原因,我不知道字符串长度是7而不是6,这使得将值与数据库中的值进行比较不可能