18-2 mini-web框架部分

前面WSGI讲web服务器和框架部分分开来,作为一个后端人员,仅需要构造框架,就是需要调用什么函数,把基本框架写出来。然后函数里面的功能,由flask和Django这些编写,由前端人员来开发

18.4mini-web框架-1-文件结构
文件结构

├── dynamic —存放py模块
│ └── my_web.py
├── templates —存放模板文件
│ ├── center.html
│ ├── index.html
│ ├── location.html
│ └── update.html
├── static —存放静态的资源文件
│ ├── css
│ │ ├── bootstrap.min.css
│ │ ├── main.css
│ │ └── swiper.min.css
│ └── js
│ ├── a.js
│ ├── bootstrap.min.js
│ ├── jquery-1.12.4.js
│ ├── jquery-1.12.4.min.js
│ ├── jquery.animate-colors.js
│ ├── jquery.animate-colors-min.js
│ ├── jquery.cookie.js
│ ├── jquery-ui.min.js
│ ├── server.js
│ ├── swiper.jquery.min.js
│ ├── swiper.min.js
│ └── zepto.min.js
└── web_server.py —mini web服务器

my_web.py

import time

def application(environ, start_response):
status = ‘200 OK’
response_headers = [(‘Content-Type’, ‘text/html’)]
start_response(status, response_headers)
return str(environ) + ‘==Hello world from a simple WSGI application!—>%s\n’ % time.ctime()

web_server.py(如上节所示)

18.5mini_web框架2-显示页面

dynamic/my_web.py
import time
import os

template_root = “./templates”

def index(file_name):
“”“返回index.py需要的页面内容”""
# return “hahha” + os.getcwd() # for test 路径问题
try:
file_name = file_name.replace(".py", “.html”)
f = open(template_root + file_name)
except Exception as ret:
return “%s” % ret
else:
content = f.read()
f.close()
return content

def center(file_name):
“”“返回center.py需要的页面内容”""
# return “hahha” + os.getcwd() # for test 路径问题
try:
file_name = file_name.replace(".py", “.html”)
f = open(template_root + file_name)
except Exception as ret:
return “%s” % ret
else:
content = f.read()
f.close()
return content

def application(environ, start_response):
status = ‘200 OK’
response_headers = [(‘Content-Type’, ‘text/html’)]
start_response(status, response_headers)

file_name = environ['PATH_INFO']
if file_name == "/index.py":
    return index(file_name)
elif file_name == "/center.py":
    return center(file_name)
else:
    return str(environ) + '==Hello world from a simple WSGI application!--->%s\n' % time.ctime()

18.6mini web 框架-3-替换模板

dynamic/my_web.py
import time
import os
import re

template_root = “./templates”

def index(file_name):
“”“返回index.py需要的页面内容”""
# return “hahha” + os.getcwd() # for test 路径问题
try:
file_name = file_name.replace(".py", “.html”)
f = open(template_root + file_name)
except Exception as ret:
return “%s” % ret
else:
content = f.read()
f.close()

    # --------更新-------
    data_from_mysql = "数据还没有敬请期待...."
    content = re.sub(r"\{%content%\}", data_from_mysql, content)

    return content

def center(file_name):
“”“返回center.py需要的页面内容”""
# return “hahha” + os.getcwd() # for test 路径问题
try:
file_name = file_name.replace(".py", “.html”)
f = open(template_root + file_name)
except Exception as ret:
return “%s” % ret
else:
content = f.read()
f.close()

    # --------更新-------
    data_from_mysql = "暂时没有数据,,,,~~~~(>_<)~~~~ "
    content = re.sub(r"\{%content%\}", data_from_mysql, content)

    return content

def application(environ, start_response):
status = ‘200 OK’
response_headers = [(‘Content-Type’, ‘text/html’)]
start_response(status, response_headers)

file_name = environ['PATH_INFO']
if file_name == "/index.py":
    return index(file_name)
elif file_name == "/center.py":
    return center(file_name)
else:
    return str(environ) + '==Hello world from a simple WSGI application!--->%s\n' % time.ctime()

猜你喜欢

转载自blog.csdn.net/qq_35264080/article/details/84717345