【发布时间】:2012-03-29 14:42:10
【问题描述】:
我正在运行一个带有 Python 2.5.1 的cherrypy 3.2.0 服务器,它每隔几天就会在来自 UI 的任何指令上出现以下错误,直到它被杀死并重新启动:-
[29/Mar/2012:06:37:57] HTTP Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cprequest.py", line 636, in respond
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cprequest.py", line 97, in run
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cprequest.py", line 57, in __call__
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 757, in init
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 162, in __init__
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 190, in _regenerate
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/lib/sessions.py", line 204, in generate_id
File "/usr/lib/python2.5/site-packages/CherryPy-3.2.0-py2.5.egg/cherrypy/_cpcompat.py", line 264, in random20
File "/usr/lib/python2.5/os.py", line 733, in urandom
NotImplementedError: /dev/urandom (or equivalent) not found
_cpcompat.py 有以下代码表明random.random 有一个后备,以防cherrypy 无法读取/dev/urandom,但似乎没有后备。
try:
os.urandom(20)
import binascii
def random20():
return binascii.hexlify(os.urandom(20)).decode('ascii')
except (AttributeError, NotImplementedError):
import random
# os.urandom not available until Python 2.4. Fall back to random.random.
def random20():
return sha('%s' % random.random()).hexdigest()
以下是来自os.py的代码sn-p,与上下文相关:-
if not _exists("urandom"):
def urandom(n):
"""urandom(n) -> str
Return a string of n random bytes suitable for cryptographic use.
"""
try:
_urandomfd = open("/dev/urandom", O_RDONLY)
except (OSError, IOError):
raise NotImplementedError("/dev/urandom (or equivalent) not found")
bytes = ""
while len(bytes) < n:
bytes += read(_urandomfd, n - len(bytes))
close(_urandomfd)
return bytes
同时当cherrypy无法读取/dev/urandom时,以下代码sn-p工作正常:-
python -c "import os;fd = open('/dev/urandom', 'r');print fd.read(5);fd.close()"
我有两个问题:-
- 为什么当我能够从 /dev/urandom 读取随机位时,cherrypy throwing not implemented 错误
- 为什么
os.py提高NotImplementedError时_cpcompact.py不执行except 部分。
【问题讨论】:
-
python -c "import os; print os.urandom(5)"工作吗? -
可悲的是,它确实如此,而cherrypy 说 /dev/urandom 没有实现!
-
也许权限被搞砸了?
-
@haltTm:我不知道,我只是在扔东西。
-
我敢打赌,如果您将一些调试代码放入
os.py以写入 syslog(或其他文件),这将是非常有帮助的。
标签: python session random cherrypy