解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
Python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误,在代码中加上如下几句即可
import sys
reload(sys)
sys.setdefaultencoding('utf8')
把 str 编码由 ascii 改为 utf8 (或 gb18030)
python3 区分了 unicode str 和 byte arrary,并且默认编码不再是 ascii
#!usr/bin/python
#-*-coding:utf-8 -*-
import flask
from flask import Flask
import sys
reload(sys)
sys.setdefaultencoding('utf8')
html_txt='''
<!DOCTYPE html>
<html>
<body>
<h2>收到GET请求</h2>
<form method='post'>
<input type='text' name='name' placeholder='请输入名字'/>
<input type='submit' value='发送POST请求'/>
</form>
</body>
</html>
'''
app=flask.Flask(__name__)
@app.route('/',methods=['GET','POST'])
def helo1():
if flask.request.method=='GET':
return html_txt
else:
name='name' in flask.request.form and flask.request.form['name']
if name:
return '你是:'+name
else:
return '你没有输入名字'
app.run(debug=True)