How to read / dev / random in python

I read in a book that it /dev/randomlooks like an endless file, but when I set up the following codes to see what the content looks like, it doesn't print anything.

with open("/dev/random") as f:
    for i in xrange(10):
        print f.readline()

By the way, when I tried this with help /dev/urandom, it worked.

+5
source share
4 answers

It outputs random bytes, not random strings. You donโ€™t see anything until you get a new line that will only run on average every 256 bytes. The reason it works /dev/urandomis simply because it works faster. Wait more, read less, or use /dev/urandom.

+8
source

FWIW, ( - ) os.urandom()

+14
with open("/dev/random", 'rb') as f:
    print repr(f.read(10))
+5
source

It looks like / dev / random is blocking you. Here is a worthy explanation.

http://www.onkarjoshi.com/blog/191/device-dev-random-vs-urandom/

0
source

All Articles