I am working on converting an existing program from Python2 to Python3. One of the methods in the program authenticates the user with a remote server. It will prompt the user to enter in 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 needs to be encoded. But as far as I can tell, it is already encoded? I found this question but I was having a hard time applying that to the context of this program.
This is the line giving me problems.
- + str(bytes('timestamp').encode('utf-8'))))- TypeError: string argument without an encoding
 
I have tried playing around with alternate ways of doing this, all with varying 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
 
I'm still getting started learning Python (but I have beginner to intermediate knowledge of Java), so I am not completely familiar with the language yet. Does anyone have any thoughts on what this issue might be?
Thanks!




+ str(timestamp)).hexdigest())is that possibly what needs to be done?str(bytes('timestamp').encode('utf-8'))- what the heck? What were you trying to do there?+ str(b'timestamp').hexdigest()))), I received aAttributeError: 'str' object has no attribute 'hexdigest'in return. It might be, but I'm not quite sure on this one.timestampis a name,'timestamp'is a string literal (no relation to thetimestampname)? You don't need bytes here. To convert an integer to a string in Python:str(timestamp). Useurllib.parse.urlencode()to create the query. Pass itstrobjects (Unicode in Python). You might need to call.decode('ascii', 'strict')on a.hexdigest()result, to getstrinstead ofbytes.