day55——django引入、小型django(socket包装的服务器)

day55——django引入、小型django(socket包装的服务器),第1张

概述day55 吴超老师Django总网页:https://www.cnblogs.com/clschao/articles/10526431.html 请求(网址访问,提交数据等等) request 响应(回复页面,回复数据等等) response web框架的本质及自定义web框架 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端,基于请 day55 吴超老师Django总网页:https://www.cnblogs.com/clschao/articles/10526431.html

请求(网址访问,提交数据等等) request

响应(回复页面,回复数据等等) response

web框架的本质及自定义web框架

我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端,基于请求做出响应,客户都先请求,服务端做出对应的响应,按照http协议的请求协议发送请求,服务端按照http协议的响应协议来响应请求,这样的网络通信,我们就可以自己实现Web框架了。

  通过对socket的学习,我们知道网络通信,我们完全可以自己写了,因为socket就是做网络通信用的,下面我们就基于socket来自己实现一个web框架,写一个web服务端,让浏览器来请求,并通过自己的服务端把页面返回给浏览器,浏览器渲染出我们想要的效果。在后面的学习中,大家提前准备一些文件:

    HTML文件内容如下,名称为test.HTML:  

详情看:https://www.cnblogs.com/clschao/articles/10391859.html#part_2

简单版回复HTML页面的web框架 python代码

server.py

import socketserver = socket.socket()server.bind(('127.0.0.1',8001))server.Listen()while 1:    conn,addr = server.accept()    from_brower_msg = conn.recv(1024)    path = from_brower_msg.decode('utf-8').split(' ')[1]    print(path)    conn.send(b'http/1.1 200 ok\r\n\r\n')    if path == '/':        with open('05dong.HTML','rb')as f:            data = f.read()    elif path == '/style.CSS':        with open('style.CSS','rb') as f:            data = f.read()    elif path == '/test.Js':        with open('test.Js','rb') as f:            data = f.read()    elif path == '/1.jpg':        with open('1.jpg','rb') as f:            data = f.read()    conn.send(data)    conn.close()
HTML代码

04test.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title>    <link rel="stylesheet" href="style.CSS"><!--    <link rel="icon" href="favicon.ico">--></head><body><h1>24期官网</h1><div>    <img src="1.jpg" alt=""></div></body><script src="test.Js"></script></HTML>
样式和Js等文件

style.CSS

body{    background: red;}

test.Js

alert(123);

图片文件--1.jpg

基础函数版 python代码

server.HTML

import socketfrom threading import Threadserver = socket.socket()server.bind(('127.0.0.1',8003))server.Listen()def home(conn):    with open('04test.HTML','rb') as f:        data = f.read()    conn.send(data)    conn.close()def CSS(conn):    with open('style.CSS','rb') as f:        data = f.read()    conn.send(data)    conn.close()def Js(conn):    with open('test.Js','rb') as f:        data = f.read()    conn.send(data)    conn.close()def pic(conn):    with open('1.jpg','rb') as f:        data = f.read()    conn.send(data)    conn.close()def nofount(conn):    conn.send(b'not found 404!')    conn.close()def ico(conn):    with open('favicon.ico','rb') as f:        data = f.read()    conn.send(data)    conn.close()urlpatterns = [    ('/',home),('/style.CSS',CSS),('/test.Js',Js),('/1.jpg',pic),('/favicon.ico',ico),]while 1:    conn,addr = server.accept()    from_brower_msg = conn.recv(1024)    path = from_brower_msg.decode('utf-8').split(' ')[1]    print(path)    conn.send(b'http/1.1 200 ok\r\n\r\n')    for url in urlpatterns:        if path == url[0]:            # ret = url[1]()            t = Thread(target=url[1],args=(conn,))            t.start()            break    else:        t = Thread(target=nofount,))        t.start()
HTML代码

04test.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title>    <link rel="stylesheet" href="style.CSS">    <link rel="icon" href="favicon.ico"></head><body><div>    <img src="1.jpg" alt=""></div><script src="test.Js"></script></body></HTML>
样式和Js等文件

style.CSS

body{    background: red;}

test.Js

alert(123);

图片文件--1.jpg

进阶函数版 python文件

server.py

import socketfrom urls import urlpatternsfrom threading import Threadimport vIEwsserver = socket.socket()server.bind(('127.0.0.1',8003))server.Listen()while 1:    conn,addr = server.accept()    from_brower_msg = conn.recv(1024)    path = from_brower_msg.decode('utf-8').split(' ')[1]    print(path)    conn.send(b'http/1.1 200 ok\r\n\r\n')    for url in urlpatterns:        if path == url[0]:            t = Thread(target=url[1],))            t.start()            break    else:        t = Thread(target=vIEws.nofount,))        t.start()

列表文件--urls.py

import vIEwsurlpatterns = [    ('/',vIEws.home),vIEws.CSS),vIEws.Js),vIEws.pic),vIEws.ico),]

函数文件--vIEws.py

def home(conn):    with open('05dong.HTML','rb') as f:        data = f.read()    conn.send(data)    conn.close()
HTML文件

05dong.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title>    <link rel="stylesheet" href="style.CSS"><!--    <link rel="icon" href="favicon.ico">--></head><body><h1>24期官网</h1><div>    <img src="1.jpg" alt=""></div></body><script src="test.Js"></script></HTML>
样式和Js等文件

style.CSS

body{    background: red;}

test.Js

alert(123);

图片文件--1.jpg

动态页面的web框架 python文件

server.py

import socketserver = socket.socket()server.bind(('127.0.0.1',8003))server.Listen()while 1:    username = "朱凡宇"    conn,addr = server.accept()    from_brower_msg = conn.recv(1024)    path = from_brower_msg.decode('utf-8').split(" ")[1]    conn.send(b'http/1.1 200 ok\r\n\r\n')    with open('dong.HTML','r',enCoding='utf-8')as f:        data = f.read()    data = data.replace('%xxoo%',username)    conn.send(data.encode())    conn.close()
HTML文件

dong.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title></head><body><h1>欢迎来到%xxoo%的首页</h1></body></HTML>
不同HTML页面的web框架 python文件

server

import socketfrom urls import urlpatternsfrom threading import Threadimport vIEwsserver = socket.socket()server.bind(('127.0.0.1',))            t.start()            break    else:        t = Thread(target=vIEws.nofount,))        t.start()

urls

import vIEwsurlpatterns = [    ('/',('/center',vIEws.person_center),]

vIEws

def home(conn):    with open('05dong.HTML','rb') as f:        data = f.read()    conn.send(data)    conn.close()def person_center(conn):    with open('center.HTML','rb') as f:        data = f.read()    conn.send(data)    conn.close()def nofount(conn):    conn.send(b'not found 404!')    conn.close()
HTML文件

05dong.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title></head><body><h1>欢迎24期来到xx网站</h1><a href="/center">个人中心</a></body></HTML>

center.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title></head><body><h1>欢迎来到xx个人中心</h1></body></HTML>
wsgiref模块 python文件

server

from wsgiref.simple_server import make_serverfrom urls import urlpatternsdef application(environ,start_response):    # environ  所有请求相关信息    # start_response --封装响应数据格式    path = environ['PATH_INFO']    start_response('200 ok',[])  # conn.send(b'http/1.1 200 ok\r\n\r\n')    for url in urlpatterns:        if path == url[0]:            ret = url[1]()            break    return [ret]if __name__ == '__main__':    h = make_server('127.0.0.1',8080,application)    h.serve_forever()

urls

import vIEwsurlpatterns = [    ('/',]

vIEws

def home():    with open('05dong.HTML','rb') as f:        data = f.read()    return data
HTML文件

05dong.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title></head><body><h1>欢迎24期来到xx网站</h1></body></HTML>
jinja2模块 python文件

server

import socketfrom urls import urlpatternsfrom threading import Threadimport vIEwsserver = socket.socket()server.bind(('127.0.0.1',))            t.start()            break

urls

import vIEwsurlpatterns = [    ('/',# ('/center',# ('/style.CSS',# ('/test.Js',# ('/1.jpg',# ('/favicon.ico',]

vIEws

from jinja2 import Templatedef home(conn):    # {% for i in hobby %}    # <li>{{ i }}</li>    # {% endfor %}    with open('08jinja2test.HTML',enCoding='utf-8') as f:        data = f.read()    t = Template(data)    ret = t.render({'hobby':['金钱','女人','张达'],'name': '朱凡宇'})    conn.send(ret.encode('utf-8'))    conn.close()
HTML文件

08jinja2test.HTML

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Title</Title></head><body><h1>{{ name }}个人介绍</h1>我的爱好<ul>    {% for i in hobby %}    <li>{{ i }}</li>    {% endfor %}</ul></body></HTML>
总结

以上是内存溢出为你收集整理的day55——django引入、小型django(socket包装的服务器)全部内容,希望文章能够帮你解决day55——django引入、小型django(socket包装的服务器)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1190908.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存