This code used to work in py2. In py3 I get this:
Here is the code:
from Crypto import Random
import os
def generate_random_bytes(length):
return Random.get_random_bytes(length)
#return bytearray(os.urandom(length)) I tried this line but got the same result
def generate_server_id():
"""Generates 20 random hex characters"""
print(generate_random_bytes(10))
print(ord(c) for c in generate_random_bytes(10))
return "".join("%02x" % ord(c) for c in generate_random_bytes(10))
Just replace ord(c)
with c
, in Py3 a byte array item is already an integer instead of a character, so you don't need the conversion.
You may want to rewrite it like this, in order to make it work on both versions:
def generate_server_id():
"""Generates 20 random hex characters"""
try:
return "".join("%02x" % ord(c) for c in generate_random_bytes(10))
except TypeError:
return "".join("%02x" % c for c in generate_random_bytes(10))