【发布时间】:2016-08-22 01:22:38
【问题描述】:
我想通过修改 Content-Type 标头来指定 HTTP 响应字符集。但是,它不起作用。
这是一个简短的例子:
#coding=utf-8
import cherrypy
class Website:
@cherrypy.expose()
def index(self):
cherrypy.response.headers['Content-Type']='text/plain; charset=gbk'
return '。。。'.encode('gbk')
cherrypy.quickstart(Website(),'/',{
'/': {
'tools.response_headers.on':True,
}
})
当我访问该页面时,Content-Type 神秘地更改为text/plain;charset=utf-8,导致浏览器中出现 mojibake。
C:\Users\Administrator>ncat 127.0.0.1 8080 -C
GET / HTTP/1.1
Host: 127.0.0.1:8080
HTTP/1.1 200 OK
Server: CherryPy/7.1.0
Content-Length: 6
Content-Type: text/plain;charset=utf-8
Date: Mon, 22 Aug 2016 01:08:13 GMT
。。。^C
CherryPy 似乎会检测内容编码并自动覆盖字符集。如果是这样,我该如何禁用此功能?
【问题讨论】:
标签: python character-encoding cherrypy