Django底層原理簡介與安裝
阿新 • • 發佈:2018-08-28
end else serve 端口 進行 ftime 處理 客戶 模擬
Django環境目錄搭建一欄:
利用wsgiref模塊封裝好的socket搭建服務端:
#利用wsgiref模塊封裝好的socket演示操作(例如accept\recv) #也可以實現socket服務端的功能 from wsgiref.simple_server import make_server #拆分開之後需要導入關聯相應表格 import my_urls from views import * #分到views裏面 # def index(response): # return ‘ok‘ # # def time(response): # return ‘time‘# # def error(response): # return ‘404‘ #分到my_urls裏面 # urls=[ # (‘/index‘,index), # (‘/time‘,time), # # ] def my_server(environ,start_response): #這裏就不需要再accept、recv了,因為封裝好了,只需要對接收數據進行處理就好 #environ是個字典:把請求頭裏所有的東西都切割出來了,不用再手動進行處理 #start_response是個函數:響應頭信息HTTP/1.1 200 OK\r\nContent-Type:text/html\r\n\r\n#取請求的地址: # print(environ[‘PATH_INFO‘]) # ‘PATH_INFO‘: ‘/ ‘,默認是空 類似於:GET /index HTTP/1.1 訪問時輸入什麽就會返回什麽 # conn.send(b‘HTTP/1.1 200 OK\r\nContent-Type:text/html\r\n\r\n‘)類似於這個功能 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) # return [b‘Hello wsgiref_socket‘] #到這裏就可以模擬MyWeb.py裏面的socket服務端功能了func=None #遍歷取出路由列表裏面的各個元組值 # for url in urls: for url in my_urls.urls: #判斷用戶請求的地址是不是寫在路由裏面的地址 if url[0] == environ[‘PATH_INFO‘]: func=url[1] #相當與 func=index 或者 func=time break #判斷是否為空 if func: response=func(environ) #即 index(environ) else: response=error(environ) #wsgiref規定返回值就是要寫在列表裏面 return [response,] if __name__ == ‘__main__‘: my=make_server(‘127.0.0.1‘,8002,my_server) print(‘監聽8002端口‘) my.serve_forever()
my_urls路由配置:
import views urls=[ (‘/index‘,views.index), (‘/time‘,views.time), (‘/user_list‘,views.user_list), (‘/favicon.ico‘,views.favicon) ]
templates渲染html模板(和路由層對應):
1.index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index界面</title> </head> <body> <img src="https://goss.veer.com/creative/vcg/veer/1600water/veer-136737644.jpg" alt="index"> </body> </html>
2.time.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> @@time@@ </body> </html>
3.user_list.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶列表</title> <style> th { background: gray; } td { border: 1px solid black; width: 200px; height: 50px; text-align: center; } th { border: 1px solid black; width: 200px; height: 50px; text-align: center; } </style> </head> <body> <table style="border: 1px solid black;border-collapse: collapse;"> <thead> <tr> <th>id</th> <th>用戶名</th> <th>密碼</th> </tr> </thead> <tbody> {% for user in user_list %} <tr> <td>{{user.id}}</td> <td>{{user.name}}</td> <td>{{user.password}}</td> </tr> {%endfor%} </tbody> </table> </body> </html>
views視圖配置:
import pymysql #jinja2是模板渲染(用於去渲染user_list.html中的文件) from jinja2 import Template #把wsgiref裏面的time index error 函數拆分過來 def index(response): # return ‘ok‘ #從template裏面調取網頁(裏面設定要響應返回給客戶端的內容) with open(‘templates/index.html‘,‘r‘,encoding=‘utf-8‘) as f: data=f.read() return data.encode(‘utf-8‘) def time(response): # return ‘time‘ import datetime now = datetime.datetime.now().strftime(‘%Y-%m-%d %X‘) with open(‘templates/two.html‘, ‘r‘, encoding=‘utf-8‘) as f: data = f.read() data = data.replace(‘@@time@@‘,now) return data.encode(‘utf-8‘) def error(response): return ‘404‘.encode(‘utf-8‘) def favicon(request): with open(‘favicon.ico‘,‘rb‘) as f: data=f.read() return data def user_list(response): conn=pymysql.connect( host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘root‘, database=‘test‘ ) #不傳參拿到是元組,傳參之後得到的是字典 cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute(‘select * from user‘) user_list=cursor.fetchall() print(user_list) #user_list列表[{‘id‘:1,‘name‘:yangzhizong,‘password‘:123},{...}] #取到user_list.html模板 with open(‘templates/user_list.html‘,‘r‘,encoding=‘utf-8‘) as f: data=f.read() #jinja2的應用,生成一個模板對象,需要傳字符串 template=Template(data) #template是個對象,就是user_list.html data=template.render(user_list=user_list) #調用對象的render方法,傳入參數完成對html文件的渲染 return data.encode(‘utf-8‘)
待完善。。。
Django底層原理簡介與安裝