Print python os.urandom output on terminal

how can I print the output of os.urandom(n) in the terminal?

I am trying to create SECRET_KEY with fabfile and will output 24 bytes.

An example of how I implement both options in the python shell:

 >>> import os >>> out = os.urandom(24) >>> out 'oS\xf8\xf4\xe2\xc8\xda\xe3\x7f\xc75*\x83\xb1\x06\x8c\x85\xa4\xa7piE\xd6I' >>> print out oS       5*      piE I 
+6
source share
2 answers

If you need a hexadecimal string, use binascii.a2b_hex (or hexlify ):

 >>> out = 'oS\xf8\xf4\xe2\xc8\xda\xe3\x7f\xc75*\x83\xb1\x06\x8c\x85\xa4\xa7piE\xd6I' >>> import binascii >>> print binascii.hexlify(out) 6f53f8f4e2c8dae37fc7352a83b1068c85a4a7706945d649 
+7
source

To use only the built-in modules, you can get the integer value with ord and then convert it to a hexadecimal number:

 list_of_hex = [str(hex(ord(z)))[2:] for z in out] print " ".join(list_of_hex) 

If you just need a hexadecimal list, then str() and [2:] not needed

The output of this and hexify() versions is of type str and should work fine for a web application.

+1
source

Source: https://habr.com/ru/post/951785/


All Articles