Can';t在Python2中设置HTTP CGI服务器
有人注意到了吗Can';t在Python2中设置HTTP CGI服务器,python,python-2.7,cgi,Python,Python 2.7,Cgi,有人注意到了吗 #!/usr/bin/env python # -*- coding: utf-8 -*- from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler class Handler(CGIHTTPRequestHandler): cgi_directories = ["/"] httpd = HTTPServer(("", 8000), Handler)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
class Handler(CGIHTTPRequestHandler):
cgi_directories = ["/"]
httpd = HTTPServer(("", 8000), Handler)
httpd.serve_forever()
以及完整的回溯:
Traceback (most recent call last):
File "server.py", line 6, in <module>
from CGIHTTPServer import CGIHTTPRequestHandler
File "C:\Python27\lib\CGIHTTPServer.py", line 30, in <module>
import SimpleHTTPServer
File "C:\Python27\lib\SimpleHTTPServer.py", line 27, in <module>
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
File "C:\Python27\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHan
ler
mimetypes.init() # try to read system mime.types
File "C:\Python27\lib\mimetypes.py", line 358, in init
db.read_windows_registry()
File "C:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
for subkeyname in enum_types(hkcr):
File "C:\Python27\lib\mimetypes.py", line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 2: ordinal
not in range(128)
回溯(最近一次呼叫最后一次):
文件“server.py”,第6行,在
从CGIHTTPServer导入cgihtprequesthandler
文件“C:\Python27\lib\CGIHTTPServer.py”,第30行,在
导入SimpleHTTPServer
文件“C:\Python27\lib\SimpleHTTPServer.py”,第27行,在
类SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
SimpleHTTPRequestHan中的文件“C:\Python27\lib\SimpleHTTPServer.py”,第208行
勒尔
mimetypes.init()#尝试读取系统mime.types
文件“C:\Python27\lib\mimetypes.py”,第358行,在init中
db.read\u windows\u注册表()
文件“C:\Python27\lib\mimetypes.py”,第258行,在read\u windows\u注册表中
对于枚举类型(hkcr)中的子关键字名称:
文件“C:\Python27\lib\mimetypes.py”,第249行,枚举类型
ctype=ctype.encode(默认编码)#在3.x中省略!
UnicodeDecodeError:“ascii”编解码器无法解码位置2:序号中的字节0xd7
不在范围内(128)
我过去经常运行这段代码,它可以正常工作,但现在根本不行。该代码在Python3中工作得非常好(更改导入)
在Windows 8上运行Python 2.7.6(均为64位)。重新安装Python不起作用
有什么想法吗
提前感谢。已解决。这是一个问题,因为mimetypes模块导致Windows注册表中的编码。如果将来有人发现同样的问题,是解决它的补丁。如果你不想浪费时间检查,这里有一个快速的解决方案。将mimetypes模块(
Lib/mimetypes.py
)中的enum_types()
函数替换为以下函数:
from itertools import count
def enum_types(mimedb):
for i in count():
try:
yield _winreg.EnumKey(mimedb, i)
except EnvironmentError:
break
谢谢大家