Using Python 2.7 I had this code:
#Create a random 56 bit (7 byte) string
random_bytes = os.urandom(7)
random_bytes = int.from_bytes(random_bytes, byteorder="big")
Which gives the error:
AttributeError: type object 'int' has no attribute 'from_bytes'
After reading online it looks like from_bytes
is Python 3. So I tried the following:
random_bytes = os.urandom(7)
#random_bytes = int.from_bytes(random_bytes, byteorder="big")
random_bytes = struct.unpack('>16B', random_bytes)
But this gives the following error:
struct.error: unpack requires a string argument of length 16
Should the >16B
be >7B
? Even with that it seems to return a tuple.
The goal is to use random_bytes
like this:
int(str(((time_bytes << 56) | random_bytes)) + code)
Maybe this will work for you:
r = chr(0) + os.urandom(7) # pad with zeroes
struct.unpack('>q', r)
q
being the code for signed 8-byte integer (Q
is unsigned).
The result is a tuple with one element.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加