使用python的WSGI接口做的一个web应用,浏览器访问出现中文的时候显示乱码 30
server.py:#-*-coding:utf-8-*-__author__='Administrator'#server.py#从wsgiref模块导入:fromws...
server.py:
# -*- coding: utf-8 -*-
__author__ = 'Administrator'
# server.py
# 从wsgiref模块导入:
from wsgiref.simple_server import make_server
# 导入我们自己编写的application函数:
from hello import application
# 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('', 8000, application)
print "Serving HTTP on port 8000..."
# 开始监听HTTP请求:
httpd.serve_forever()
hello.py:
# -*- coding: utf-8 -*-
import sys
__author__ = 'Administrator'
def application(environ, start_response):
reload(sys)
sys.setdefaultencoding('utf8')
start_response('200 OK', [('Content-Type', 'text/html')])
path_info = environ['PATH_INFO'][1:]
#path_info = path_info.decode('gb2312').encode('gb2312')
print type(path_info)
print path_info
return '<h1>Hello, %s!</h1>' % (path_info or 'web')
============分割线===============
我想转码 但是报错 以下是我转码的代码
#path_info = path_info.decode('gb2312').encode('gb2312')
报错内容:
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/lin/share/temp/hello.py", line 10, in application
path_info = path_info.decode('gb2312').encode('gb2312')
UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 0-1: illegal multibyte sequence 展开
# -*- coding: utf-8 -*-
__author__ = 'Administrator'
# server.py
# 从wsgiref模块导入:
from wsgiref.simple_server import make_server
# 导入我们自己编写的application函数:
from hello import application
# 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('', 8000, application)
print "Serving HTTP on port 8000..."
# 开始监听HTTP请求:
httpd.serve_forever()
hello.py:
# -*- coding: utf-8 -*-
import sys
__author__ = 'Administrator'
def application(environ, start_response):
reload(sys)
sys.setdefaultencoding('utf8')
start_response('200 OK', [('Content-Type', 'text/html')])
path_info = environ['PATH_INFO'][1:]
#path_info = path_info.decode('gb2312').encode('gb2312')
print type(path_info)
print path_info
return '<h1>Hello, %s!</h1>' % (path_info or 'web')
============分割线===============
我想转码 但是报错 以下是我转码的代码
#path_info = path_info.decode('gb2312').encode('gb2312')
报错内容:
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/lin/share/temp/hello.py", line 10, in application
path_info = path_info.decode('gb2312').encode('gb2312')
UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 0-1: illegal multibyte sequence 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询