开发者学堂课程【Python入门 2020年版:读取文件并返回给浏览器】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10484
读取文件并返回给浏览器
1.代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
status_code=
’200 OK’
if path == '/':
response =
’
欢迎来到我的首页'
elif path =='/test':
response = '
欢迎来到test页面'
elif path ==
‘/demo' :
response = '
欢迎来到demo页面'
else:
status_code=
’404 Not Found’
response = '
页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
2.如何返回不同的内容
现在返回的都是字符串,那么能不能返回不同的内容呢?
把字典改成json字符串返回,再读取并返回一个文件,文件xxxx.txt先建好,里面输入:我是文件里的内容。
(1)json字符串返回
修改代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
status_code=
’200 OK’
if path == '/':
response =
’
欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
else:
status_code=
’404 Not Found’
response = '
页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行并访问:127.0.0.1:8080/test
访问结果如下:
这是装了插件以后的样子,这个插件在哪安装,更多工具里有个扩展程序,在扩展程序中的 Chrome 商店搜索 json 就能找到。
(2)读取并返回文件
写一个 html 文件,名为 hello.html。我写的大家看不懂,大家看效果就可以
效果如图:
修改代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
status_code=
’200 OK’
if path == '/':
response =
’
欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/hello' :
with open(
‘page/hello.html’,’r’,encoding=’’utf8) as file:
response = file.read()
else:
status_code=
’404 Not Found’
response
= '页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行并访问:127.0.0.1:8080/hello
访问结果如下:
3.前端给后端怎么传输数据:
访问时在地址栏加个问号输入传的数据
127.0.0.1:8080/hello?name=zhangsan &age=18,后端就能拿数据,用environ[‘QUERY_STRING’]
拿数据
(1)代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
environ[
‘QUERY_STRING’]
status_code=
’200 OK’
if path == '/':
response =
’
欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/hello' :
with open(
‘page/hello.html’,’r’,encoding=’’utf8) as file:
response = file.read()
else:
status_code=
’404 Not Found’
response
= '页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行发现,发的数据是能找到的
(2)怎么拿数据
代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
print(environ,file=open(
‘pages/xxxx.txt’,’w’,encoding=’utf8’))
status_code=
’200 OK’
if path == '/':
response
=’欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/hello' :
with open(
‘page/hello.html’,’r’,encoding=’’utf8) as file:
response = file.read()
else:
status_code=
’404 Not Found’
response
= '页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行后有[‘QUERY_STRING’]
,这是字典
(3)print(environ.get(‘QUERY_STRING’))
QUERY_STRING
==>获取到客户端GET请求方式传递过来的参数
代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
print(environ.get(
‘QUERY_STRING’))
status_code=
’200 OK’
if path == '/':
response
=’欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/hello' :
with open(
‘page/hello.html’,’r’,encoding=’’utf8) as file:
response = file.read()
else:
status_code=
’404 Not Found’
response
= '页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行就可以拿到数据
这是 GET 请求的数据获取方式,直接把参数放在地址栏里用?、&连接发消息
POST 请求数据的方式后面再说,POST 的内容是在请求体里。
4.后端给前端发消息
比如访问127.0.0.1:8080显示欢迎来到我的首页,这就是后端传的消息,访问其他的也是一样的。
我写一个文件 info.html 里面写有{username}
,欢迎回来。
(1)添加代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
print(environ.get(
‘QUERY_STRING’))
status_code=
’200 OK’
if path == '/':
response
=’欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/hello' :
with open(
‘page/hello.html’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/info' :
#查询数据库,获取到用户名
name=
’jack’
with open(
‘page/info.html’,’r’,encoding=’’utf8) as file:
response = file.read()
else:
status_code=
’404 Not Found’
response
= '页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
(2)如果查询数据库名字叫jack,怎么显示名字 jack 在页面上呢。response 是读info 文件的,读出来之后就是一个内容就是一个文本,即字符串,有个字符串‘{username},
欢迎回来’怎么把jack填进去:
from wsgiref.simple_server import make_server
response
=’欢迎来到我的首页'
elif path =='/test':
elif path ==
‘/info' :
#查询数据库,获取到用户名
name=
’jack’
with open(
‘page/info.html’,’r’,encoding=’’utf8) as file:
response = file.read().format(username=name)
#
‘{username},
欢迎回来’.format(username=name)
else:
status_code=
’404 Not Found’
response =
'页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行之后显示:jack,欢迎回来
(3)这个在后面不用写这么复杂,后面会有很多渲染模板,在讲flask、django都有模板,渲染引擎。
渲染引擎就是在页面上写很多空,在渲染时把空填上,这就是动态的页面了。
演示举例:
把info文件内容里的{username}
,欢迎回来,改成{username},
欢迎回来,你今年{age}
岁了,你的性别是{gender}
。
修改代码:
from wsgiref.simple_server import make_server
def demo_app(environ,start_response):
path=environ[
‘PATH_INFO’]
print(environ.get(
‘QUERY_STRING’))
status_code=
’200 OK’
if path == '/':
response
=’欢迎来到我的首页'
elif path =='/test':
response = json.dumps({
‘name’:’zhangsan’,’age’:18})
elif path ==
‘/demo' :
with open(
‘page/xxxx.txt’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/hello' :
with open(
‘page/hello.html’,’r’,encoding=’’utf8) as file:
response = file.read()
elif path ==
‘/info' :
#查询数据库,获取到用户名
name=
’jack’
with open(
‘page/info.html’,’r’,encoding=’’utf8) as file:
response=file.read().format(username=name ,age=18,gender=
’男’)
#
‘{username},欢迎回来’.format(username=name)
else:
status_code=
’404 Not Found’
response = '
页面走丢了’
start_response(
‘status_code’,[(‘Content-Type,’text/html;charset=utf8’)])
return [
‘response’.encode(‘utf8’)]
if_ name_=='_ main_ ':
httpd : make_server(
‘’, 8080, demo_app):
sa = httpd.socket. getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
运行访问结果就是:jack,欢迎回来,你今年18岁了,你的性别是男