How to read /dev/random in python

前端 未结 3 1926
无人共我
无人共我 2021-01-05 01:47

I read in a book that /dev/random is like an infinite file, but when I set up the following codes to see what the content look like, it prints nothing.

相关标签:
3条回答
  • 2021-01-05 02:21

    It is outputting random bytes, not random lines. You see nothing until you get a newline, which will only happen every 256 bytes on average. The reason /dev/urandom appears to work is simply that it operates faster. Wait longer, read less, or use /dev/urandom.

    0 讨论(0)
  • 2021-01-05 02:21

    FWIW, the preferred way of accessing this stream (or something like it) in a semi-portable way is os.urandom()

    0 讨论(0)
  • 2021-01-05 02:37
    with open("/dev/random", 'rb') as f:
        print repr(f.read(10))
    
    0 讨论(0)
提交回复
热议问题