如何在python中读取/dev/random [英] How to read /dev/random in python

查看:35
本文介绍了如何在python中读取/dev/random的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在书中读到 /dev/random 就像一个无限文件,但是当我设置以下代码以查看内容的样子时,它什么也没打印.

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.

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

顺便说一句,当我用 /dev/urandom 尝试这个时,它奏效了.

BTW, when I tried this with /dev/urandom, it worked.

推荐答案

它输出的是随机字节,而不是随机行.在获得换行符之前,您什么也看不到,换行符平均每 256 个字节才会发生一次./dev/urandom 似乎有效的原因很简单,因为它运行得更快.等待更长时间,阅读更少,或使用 /dev/urandom.

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.

这篇关于如何在python中读取/dev/random的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆