[Tutor] os.urandom()
Evert Rol
evert.rol at gmail.com
Sun Aug 8 00:29:25 CEST 2010
> <http://docs.python.org/py3k/library/os.html?highlight=urandom#os.urandom>
>
> using Vista, Python 3.1:
>
>>>> import os
>>>> os.urandom(6)
> b'\xd1\xfc\xb0\x14\xeaL'
>
> So what is this output? What in ascii? What in hex? Do those
> questions even make sense?
It returns just what it says in the docs: "a string of [6] random bytes"
You can convert the bytes to characters using eg chr():
>>> [a for a in map(chr, os.urandom(6))]
['Ó', 'ó', 'P', 'Ü', 'Ó', 'C']
Not all converted bytes will be printable characters, so some may appear with the \x still prepended (eg, control characters).
Does that help? What actually do you wish to achieve in the first place?
> I've tried 2 things to get at it:
>>>> import binascii
>>>> binascii.b2a_qp(b'\xd1\xfc\xb0\x14\xeaL')
> b'=D1=FC=B0=14=EAL'
>>>> binascii.b2a_hex(b'\xd1\xfc\xb0\x14\xeaL')
> b'd1fcb014ea4c'
>
> Help, please.
>
> Dick Moores
>
>
>
>
>
>>>> import binascii
>>>> binascii.b2a_qp(b'\x05\x8aU\x92\xf3R')
> b'=05=8AU=92=F3R'
>>>> binascii.b2a_hex(b'\x05\x8aU\x92\xf3R')
> b'058a5592f352'
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list