0

I have a python server and when i ask the client the login name to check it is valid with the 1 in the database, for some reason there is always a whitespace at the end of the string. I already tried this:

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

Here is a code snippet from the server:

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"))

Let's say there is an account name oliver in the data base and the client sends oliver. For some some reason i don't know is the string length 7 and not 6 and this makes comparing the value with the value from the database imposseble

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    `strip` only removes leading and trailing whitespace and `oldstring.replace('\n\t\r\s\f', '')` does not do what you think it does. In this case you're probably best using the `re` modules. Looking for a dupe to link. – pault Jan 04 '19 at 21:57
  • 1
    Possible duplicate of [How to strip all whitespace from string](https://stackoverflow.com/questions/3739909/how-to-strip-all-whitespace-from-string). Simplest thing IMO is to `import re` and do `re.sub("\s+", "", oldstring)` – pault Jan 04 '19 at 22:00
  • `re.sub()` should work but do you have a sample string? – mad_ Jan 04 '19 at 22:03
  • None of that is working. That space at the end of the string is still there – Timmy Verrept Jan 04 '19 at 22:16
  • @TimmyVerrept you're going to have to provide an [mcve]. We don't need any of the database code- show examples of strings with spaces that don't get removed. – pault Jan 04 '19 at 22:39
  • Found the problem with print(repr(accountName)). Turns out the message i receive from the client contains a \x00 I don't know why that is. – Timmy Verrept Jan 05 '19 at 01:20
  • The client may be written in a language like C which uses nul terminated strings and is incorrectly including the trailing nul when it transmits it to your server. Your code is also suspect. For example, all the `str()` invocations are almost certainly not needed. – Kurtis Rader Jan 05 '19 at 20:25

0 Answers0