Set encoding in Python 3 CGI scripts

前端 未结 7 1821
醉话见心
醉话见心 2020-12-03 03:07

When writing a Python 3.1 CGI script, I run into horrible UnicodeDecodeErrors. However, when running the script on the command line, everything works.

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 03:53

    I have encountered the same problem. My environment is Windows10 + Apache 2.4 + Python 3.8.
    As I am developing an overlay for Google Earth Pro, which only accept CGI to get dynamic content.
    In the best answer, here is the reason but the method does not work.
    My solution is:

    sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)
    

    It works well.

提交回复
热议问题