Can't set up a HTTP CGI Server in Python 2
Question: Does anyone notice this?

    
    
    #!/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()
    

And the complete traceback:

    
    
    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)
    

I used to run this code in the past and it works right, but now it simply
doesn't. The code works perfect in Python 3 (changing the imports).

Running Python 2.7.6 on Windows 8 (both 64 bits). Reinstalling Python doesn't
work.

Any ideas?

Thanks in advance.


Answer: Solved. This is an issue because of the encoding in the Windows registry
caused by the mimetypes module. If anybody find the same problem in the
future, [here](http://bugs.python.org/issue9291) is the patch to solve it. If
you don't want to waste your time checking that, here's the quick solution.
Replace the `enum_types()` function from the mimetypes module
(`Lib/mimetypes.py`) with this one:

    
    
    from itertools import count
    def enum_types(mimedb):
        for i in count():
            try:
                yield _winreg.EnumKey(mimedb, i)
            except EnvironmentError:
                break
    

Thanks to all.

