"TypeError: string argument without encoding", but the string is encoded?

I am working on converting an existing program from Python2 to Python3. One of the methods in the program authenticates the user from a remote server. It will prompt the user to enter a password.

def _handshake(self):
    timestamp = int(time.time())
    token = (md5hash(md5hash((self.password).encode('utf-8')).hexdigest()
                + str(bytes('timestamp').encode('utf-8'))))
    auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=%s" % (self.name,
                                                          self.username,
                                                          timestamp,
                                                          token,
                                                          self.client_code)
    response = urlopen(auth_url).read()
    lines = response.split("\n")
    if lines[0] != "OK":
        raise ScrobbleException("Server returned: %s" % (response,))
    self.session_id = lines[1]
    self.submit_url = lines[3]

The problem with this method is that after the integer is converted to a string, it must be encoded. But as far as I can tell, is it already encoded? I found this question , but it was difficult for me to apply this to the context of this program.

This is the line that gives me problems.

  • + str(bytes('timestamp').encode('utf-8'))))
    • TypeError: string argument without an encoding

I tried playing with alternative ways of doing this, all with different types of errors.

  • + str(bytes('timestamp', 'utf-8'))))
    • TypeError: Unicode-objects must be encoded before hashing
  • + str('timestamp', 'utf-8')))
    • TypeError: decoding str is not supported

Python ( Java), . - , ?

!

+4
1

, python 3.

bytes("bla bla"), b"blabla" , bytes("bla bla","utf-8"), , , .

TypeError: string argument without an encoding

.

, str. , str, :

my_bytes_value.decode("utf-8")

str.

, ! !

+8

All Articles