Django框架导读
一、课程导读
1. web应用
运行在浏览器上的应用
2. c/ s b/ s 架构
client/ server:客户端服务器架构,C+ +
brower/ server:浏览器服务器架构,Java、Python
底层均是基于socket
3. Python web框架
Django socket用的wsgiref,页面路由自己写的,模板渲染自己写的,功能全面
Flask socket用的第三方,页面路由自己写的,模板渲染自己写的,小而轻
Tornado socket自己写的,页面路由自己写的,模板渲染自己写的,支持高并发
二、原生socket服务
import socket
PORT = 8801
server = socket. socket( )
server. bind( ( '127.0.0.1' , PORT) )
server. listen( 5 )
print ( "服务端启动:http://127.0.0.1:%s" % PORT)
while True :
browser, _ = server. accept( )
data = browser. recv( 1024 ) . decode( 'utf-8' )
print ( data)
browser. send( b'HTTP/1.1 200 OK\r\n' )
browser. send( b'Content-type:text/html\r\n' )
browser. send( b'\r\n' )
browser. send( b'normal msg' )
browser. send( b'<h1>hello world</h1>' )
browser. close( )
'''
# 请求发来的数据
GET / HTTP/1.1
Host: 127.0.0.1:8801
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
'''
三、http协议
1 . 什么是http协议:
2. http工作原理( 事务)
3 . 请求报文
'''
POST / HTTP/1.1\r\n
Host: 127.0.0.1:8001\r\n
Connection: keep-alive\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n
Accept-Encoding: gzip, deflate, br\r\n
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\r\n
\r\n
usr=abc&pwd=123
'''
4 . 响应报文
'''
HTTP/1.1 200 OK\r\n
Content-type:text/html\r\n
\r\n
Login Success
'''
5 . 状态码
四、项目演变
part4
- - keji. jpg
- - login. html
- - server4. py
- - urls. py
- - views. py
< !DOCTYPE html>
< html>
< head>
< meta charset= "utf-8" >
< title> login result< / title>
< / head>
< body>
< !- - my login page = > % % res% % - - >
my login page = > { { result } }
< hr>
{ { my_dic} }
< hr>
{ % for k in my_dic % }
< div> { { k} } : { { my_dic[ k] } } < / div>
{ % endfor % }
< / body>
< / html>
from wsgiref import simple_server
from part4. urls import urls
PORT = 8804
def app ( environ, response) :
print ( environ)
response( "200 OK" , [ ( 'Content-type' , 'text/html' ) ] )
url = environ[ 'PATH_INFO' ]
res_dic = { }
if environ. get( 'REQUEST_METHOD' , None ) == "GET" and url == "/login" :
res_str = environ[ 'QUERY_STRING' ]
if res_str:
res_list = res_str. split( '&' )
for k_v_m in res_list:
k_v_l = k_v_m. split( '=' )
res_dic[ k_v_l[ 0 ] ] = k_v_l[ 1 ]
resp_data = b'404'
if url in urls:
resp_data = urls[ url] ( res_dic)
return [ resp_data]
if __name__ == '__main__' :
server = simple_server. make_server( '127.0.0.1' , PORT, app)
print ( "服务启动:http://127.0.0.1:%s" % PORT)
server. serve_forever( )
from part4. views import *
urls = {
'/' : index,
'/index' : index,
'/favicon.ico' : ico,
'/login' : login,
}
import pymysql
from jinja2 import Template
def ico ( dic= None ) :
with open ( 'keji.jpg' , 'rb' ) as f:
ico_data = f. read( )
return ico_data
def index ( dic= None ) :
return b'home page'
pass
def login ( dic= None ) :
print ( dic)
usr = dic[ 'usr' ]
pwd = dic[ 'pwd' ]
conn = pymysql. connect(
host= '192.168.32.100' ,
db= 'dg1' ,
user= 'root' ,
passwd= '123456'
)
cursor = conn. cursor( pymysql. cursors. DictCursor)
row = cursor. execute( 'select * from user where usr=%s and pwd = %s' , [ usr, pwd] )
with open ( 'login.html' , 'rt' ) as f:
msg = f. read( )
tem = Template( msg)
if row:
res = 'login success'
else :
res = 'login failed'
msg = tem. render( result= res, my_dic= dic)
return msg. encode( 'utf-8' )