码迷,mamicode.com
首页 > 编程语言 > 详细

基于python的web编程基础

时间:2018-05-25 10:57:36      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:--   turn   hello   UNC   int   func   web   web编程   获取   

一、框架分类

     1、sockect+业务处理

      该类框架有:tornado

技术分享图片

2、只有业务逻辑,不包含socket,而是引用第三方组件(socket),这个第三方组件(socket)要遵循的协议就是:wsgi

框架有:django、bottle、flask  主要是逻辑处理、第三方的模块(包含了socket)

3、python中内置了wsgiref,它包含了socket

4、WEB框架原理

技术分享图片

二、框架讲解

第一步:

from wsgiref.simple_server import make_server
def new():
    return new

def index():
    return index

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(200 OK, [(Content-Type, text/html)])
    #获取用户URL
    url =  environ[PATH_INFO]
    if url == "/new":
        ret = new()
    elif url == "/index":
        ret = index()
    else:
        ret = "404"
    return ret


if __name__ == __main__:
    httpd = make_server(‘‘, 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()

运行结果

技术分享图片

 

技术分享图片

 

技术分享图片

 

 改进版

from wsgiref.simple_server import make_server
def new():
    return new

def index():
    return index

URLS = {
    "/new": new,
    "/index": index,
}

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(200 OK, [(Content-Type, text/html)])
    #获取用户URL
    url =  environ[PATH_INFO]
    # if url == "/new":
    #     ret = new()
    # elif url == "/index":
    #     ret = index()
    # else:
    #     ret = "404"
    # return ret
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == __main__:
    httpd = make_server(‘‘, 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()

再进化

from wsgiref.simple_server import make_server
def new():
    return <html><head></head><body><h1 style="color:red;">asdf</h1></body></html>

def index():
    return index

URLS = {
    "/new": new,
    "/index": index,
}

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(200 OK, [(Content-Type, text/html)])
    #获取用户URL
    url =  environ[PATH_INFO]
    # if url == "/new":
    #     ret = new()
    # elif url == "/index":
    #     ret = index()
    # else:
    #     ret = "404"
    # return ret
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == __main__:
    httpd = make_server(‘‘, 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()
运行结果:
技术分享图片

 再向前进化一步

结构:技术分享图片

from wsgiref.simple_server import make_server
def new():
    f = open(new.html,r)
    data = f.read()
    return data

def index():
    return index

URLS = {
    "/new": new,
    "/index": index,
}

#用户请求所有内容封装在变量:environ
#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(200 OK, [(Content-Type, text/html)])
    #获取用户URL
    url =  environ[PATH_INFO]
    # if url == "/new":
    #     ret = new()
    # elif url == "/index":
    #     ret = index()
    # else:
    #     ret = "404"
    # return ret
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == __main__:
    httpd = make_server(‘‘, 8000, RunServer)
    print("Serving HTTP on port 8000...")
    httpd.serve_forever()

运行结果:技术分享图片

 

再向前进化

技术分享图片

技术分享图片

V-- new.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>hello you</h1>
</body>
</html>

C -- controller.py
def new():
    f = open(new.html,r)
    data = f.read()
    return data

def index():
    return index

def home():
    return home

路由:urls
import  controller

URLS = {
    "/new": controller.new,
    "/index": controller.index,
    "/home": controller.home,
}

启动程序:start.py
from wsgiref.simple_server import make_server
from urls import URLS

#返回给用户所有信息变量:start_response
def RunServer(environ, start_response):
    start_response(200 OK, [(Content-Type, text/html)])
    #获取用户URL
    url =  environ[PATH_INFO]
    if url in URLS.keys():
        func_name = URLS[url]
        ret = func_name()
    else:
        ret = "404"
    return ret


if __name__ == __main__:
    httpd = make_server(‘‘, 8000, RunServer)
    httpd.serve_forever()

进一步升级:模板

技术分享图片

技术分享图片

 

看代码:

import os
import time
def new():
    f = open(os.path.join(views,new.html),r)
    data = f.read()
    new_data = data.replace({{itme}},str(time.time()))
    return new_data

def index():
    f = open(os.path.join(views, index.html), r)
    data = f.read()
    return data

def home():
    f = open(os.path.join(views, home.html), r)
    data = f.read()
    return data

 下面引入模块语言:第三方组件jingjia2    安装:pip3 install jingjia2

技术分享图片

技术分享图片

技术分享图片

 

基于python的web编程基础

标签:--   turn   hello   UNC   int   func   web   web编程   获取   

原文地址:https://www.cnblogs.com/xkjlive/p/9086249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!