錯誤提示:
C:\Users\Dmail\AppData\Local\Programs\Python\Python37\python.exe "E:/s9/day60/06 jinja2版web服務端示例.py"
我再8090等你哦...
Traceback (most recent call last):
File "C:\Users\Dmail\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Users\Dmail\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 184, in finish_response
self.write(data)
File "C:\Users\Dmail\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 279, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [29/Sep/2019 13:13:30] "GET /index/ HTTP/1.1" 500 59
127.0.0.1 - - [29/Sep/2019 13:13:30] "GET /favicon.ico HTTP/1.1" 200 14
錯誤代碼:
from wsgiref.simple_server import make_server
from jinja2 import Template
def index():
with open('06jinja2版web框架.html','r',encoding='utf-8')as f:
data = f.read()
template = Template(data) # 生成模板文件
ret = template.render({"name":"Dmail","hobby_list":['編程','大寶劍']}) #把數據填充到模板里面
return [ret.encode('utf-8'),]
url_func = [
('/index/',index)]
def run_server(environ,star_response):
star_response('200 OK',[('Content-Type','text/html;charset=urf-8')])
url = environ['PATH_INFO'] #取到用戶輸入的url
#print(url)
func = None
for i in url_func:
if i[0] == url:
func = i[1]
break
if func:
response = func()
else:
response = b'404 not found!'
return response
if __name__ == '__main__':
httpd = make_server('127.0.0.1',8090,run_server)
print('我再8090等你哦...')
httpd.serve_forever()
run_server函數執行到
response = func():
...
return [ret.encode('utf-8'),]
得到的返回值是一個列表
發現返回值給錯了,應該是給一個bytes
將最后給返回值的代碼改成
return ret.encode('utf-8')
訪問:127.0.0.1:8090/index/之后可以正常訪問了,但還是會報錯
再將
run_server函數里的return改一下
return [response,]
好了,可以正常訪問了,而且沒有報錯。
修改后的代碼:
from wsgiref.simple_server import make_server
from jinja2 import Template
def index():
with open('06jinja2版web框架.html','r',encoding='utf-8')as f:
data = f.read()
template = Template(data) # 生成模板文件
ret = template.render({"name":"Dmail","hobby_list":['編程','大寶劍']}) #把數據填充到模板里面
return ret.encode('utf-8')
url_func = [
('/index/',index)]
def run_server(environ,star_response):
star_response('200 OK',[('Content-Type','text/html;charset=urf-8')])
url = environ['PATH_INFO'] #取到用戶輸入的url
#print(url)
func = None
for i in url_func:
if i[0] == url:
func = i[1]
break
if func:
response = func()
else:
response = b'404 not found!'
return [response,]
if __name__ == '__main__':
httpd = make_server('127.0.0.1',8090,run_server)
print('我再8090等你哦...')
httpd.serve_forever()
服務端日志:
C:\Users\Dmail\AppData\Local\Programs\Python\Python37\python.exe "E:/s9/day60/06 jinja2版web服務端示例.py" 我再8090等你哦... 127.0.0.1 - - [29/Sep/2019 13:27:37] "GET /index/ HTTP/1.1" 200 213 127.0.0.1 - - [29/Sep/2019 13:27:37] "GET /favicon.ico HTTP/1.1" 200 14 127.0.0.1 - - [29/Sep/2019 13:27:40] "GET /index/ HTTP/1.1" 200 213 127.0.0.1 - - [29/Sep/2019 13:27:40] "GET /favicon.ico HTTP/1.1" 200 14
PS:報錯啥原因我也一臉懵逼,不太清楚,反正搜索谷歌,百度啊,修修改改就改好了。emmm,記個博客吧,怕以后遇到類似的錯誤。
