彻底解决python cgi 编程出现的编码问题
Answering this for late-comers because I don't think that the posted answers get to the root of the problem, which is the lack of locale environment variables in a CGI context. I'm using Python 3.2.
- open() opens file objects in text (string) or binary (bytes) mode for reading and/or writing; in text mode the encoding used to encode strings written to the file, and decode bytes read from the file, may be specified in the call; if it isn't then it is determined by locale.getpreferredencoding(), which on linux uses the encoding from your locale environment settings, which is normally utf-8 (from e.g. LANG=en_US.UTF-8) - >>> f = open('foo', 'w') # open file for writing in text mode
 >>> f.encoding
 'UTF-8' # encoding is from the environment
 >>> f.write('€') # write a Unicode string
 1
 >>> f.close()
 >>> exit()
 user@host:~$ hd foo
 00000000 e2 82 ac |...| # data is UTF-8 encoded
- sys.stdout is in fact a file opened for writing in text mode with an encoding based on locale.getpreferredencoding(); you can write strings to it just fine and they'll be encoded to bytes based on sys.stdout's encoding; print() by default writes to sys.stdout - print() itself has no encoding, rather it's the file it writes to that has an encoding; - >>> sys.stdout.encoding
 'UTF-8' # encoding is from the environment
 >>> exit()
 user@host:~$ python3 -c 'print("€")' > foo
 user@host:~$ hd foo
 00000000 e2 82 ac 0a |....| # data is UTF-8 encoded; \n is from print()- ; you cannot write bytes to sys.stdout - use sys.stdout.buffer.write() for that; if you try to write bytes to sys.stdout using sys.stdout.write() then it will return an error, and if you try using print() then print() will simply turn the bytes object into a string object and an escape sequence like - \xffwill be treated as the four characters \, x, f, f- user@host:~$ python3 -c 'print(b"\xe2\xf82\xac")' > foo
 user@host:~$ hd foo
 00000000 62 27 5c 78 65 32 5c 78 66 38 32 5c 78 61 63 27 |b'\xe2\xf82\xac'|
 00000010 0a |.|
- in a CGI script you need to write to sys.stdout and you can use print() to do it; but a CGI script process in Apache has no locale environment settings - they are not part of the CGI specification; therefore the sys.stdout encoding defaults to ANSI_X3.4-1968 - in other words, ASCII; if you try to print() a string that contain non-ASCII characters to sys.stdout you'll get "UnicodeEncodeError: 'ascii' codec can't encode character...: ordinal not in range(128)" 
- a simple solution is to pass the Apache process's LANG environment variable through to the CGI script using Apache's mod_env PassEnv command in the server or virtual host configuration: PassEnv LANG; on Debian/Ubuntu make sure that in /etc/apache2/envvars you have uncommented the line ". /etc/default/locale" so that Apache runs with the system default locale and not the C (Posix) locale (which is also ASCII encoding); the following CGI script should run without errors in Python 3.2: - #!/usr/bin/env python3
 import sys
 print('Content-Type: text/html; charset=utf-8')
 print()
 print('<html><body><pre>' + sys.stdout.encoding + '</pre>h€lló wörld<body></html>')
https://stackoverflow.com/questions/9322410/set-encoding-in-python-3-cgi-scripts
彻底解决python cgi 编程出现的编码问题的更多相关文章
- Python CGI编程(转自易百)
		Python CGI编程 Python的CGI编程,公共网关接口或CGI,Web服务器和一个自定义的脚本之间交换信息是一组定义的标准. 什么是CGI ? 公共网关接口或CGI,Web服务器和一 ... 
- python CGI编程-----简单的本地使用(1)
		本章节需要安装python开发工具,window平台安装地址:https://www.python.org/downloads/windows/,linux安装地址:https://www.pytho ... 
- 转:python cgi编程
		转:http://www.runoob.com/Python/python-cgi.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway ... 
- 吴裕雄--天生自然python学习笔记:Python CGI编程
		什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ... 
- python CGI 编程实践
		文章更新于:2020-03-05 注1:安装 python 参见: python 的安装使用和基本语法 注2:配置 web 环境参见: Windows&linux使用集成环境搭建 web 服务 ... 
- Python  CGI编程和CGIHTTPServer
		Python2.7 的CGIHTTPServer 可以作为一个简单的HTTP服务器,能够调用cgi脚本 1 在任意目录下创建一个特殊的目录 cgi-bin ,用于存放自己写的脚本(.py或.cgi) ... 
- Python基础编程:字符编码、数据类型、列表
		目录: python简介 字符编码介绍 数据类型 一.Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心 ... 
- Python CGI编程
		CGI(Common Gateway Interface)通用网关接口,它是一段程序,运行在服务器上.如:HTTP服务器,提供同客户端HTML页面的接口. CGI程序可以是python脚本,PERL脚 ... 
- python CGI编程Apache配置
		1. 编辑http.conf,添加两行,路径可以自定义 <Directory "C:/AppServ/www/cgi-bin"> AllowOverride None ... 
随机推荐
- DDX和DDV——控件与变量之间值的传递
			DoDataExchange由框架调用,作用是交互并且验证对话框数据,主要由(DDX) 和 (DDV)宏实现. 永远不要直接调用这个函数,而是通过UpdateData(TRUE/FALSE)实现控件与 ... 
- CSS3基础知识学习
			CSS3动画例子展示 http://www.17sucai.com/pins/demoshow/13948 HTML5和CSS3特效展示 http://www.html5tricks.com/30-m ... 
- 解决部分浏览器不能显示itext生成的PDF文件文本域内容问题
			利用Itext可以实现pdf的高效动态生成,但在实践过程中遇到了一个问题: 即itext利用map中的值设置到pdf模板上建立的文本域中时:能成功生成,但是在部分浏览器上(360,QQ,等浏览器)无法 ... 
- leetcode378 Kth Smallest Element in a Sorted Matrix
			思路1: 使用堆. 实现: class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ... 
- Sql 行转换为列  以及列转换为行的心得
			这是 创建数据库的脚本文件 CREATE TABLE [dbo].[stu]( [学号] [nvarchar](255) NOT NULL, [姓名] [nvarchar](255) NULL, [性 ... 
- Java中枚举类型Enum的一种使用方式
			枚举类定义如下: public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败 ... 
- 清理ThreadLocal
			在我很多的课程里(master.concurrency.xj-conc-j8),我经常提起ThreadLocal.它经常受到我严厉的指责要尽可能的避免使用.ThreadLocal是为了那些使用完就销毁 ... 
- IOS自动化测试之UIAutomation
			通过Xcode工具编写运行测试脚本 1.当你有了一个应用的源代码之后,在Xcode工具中,首先选中被测应用,然后点击菜单栏中的“Product-Profile”,则会弹出Instruments工具,在 ... 
- 插值(scipy.interpolate)
			https://docs.scipy.org/doc/scipy/reference/interpolate.html#module-scipy.interpolate https://stackov ... 
- (六)使用Docker镜像(下)
			1. 创建镜像 创建镜像的方法有三种: 基于已有镜像的容器创建 基于本地模板导入 基于Dockerfile创建 1.1 基于已有镜像的容器创建 该方法主要是使用docker commit命令,其格式 ... 
