最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)手寫一個類似django的web框架示例

 更新時間:2018年07月20日 09:42:43   作者:鎧甲巨人  
這篇文章主要介紹了Python實現(xiàn)手寫一個類似django的web框架,結合具體實例形式分析了Python自定義簡單控制器、URL路由、視圖模型等功能,實現(xiàn)類似Django框架的web應用相關操作技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)手寫一個類似django的web框架。分享給大家供大家參考,具體如下:

用與django相似結構寫一個web框架。

啟動文件代碼:

from wsgiref.simple_server import make_server #導入模塊
from views import *
import urls
def routers():  #這個函數(shù)是個元組
  URLpattern=urls.URLpattern
  return URLpattern #這個函數(shù)執(zhí)行后返回這個元組
def application(environ,start_response):
  print("ok1")
  path=environ.get("PATH_INFO")
  print("path",path)
  start_response('200 OK',[('Content-Type','text/html')])
  urlpattern=routers() #講函數(shù)的返回值元組賦值
  func=None
  for item in urlpattern: #遍歷這個元組
    if path==item[0]:  #item[0]就是#路徑后面的斜杠內(nèi)容
      func=item[1]  #item[1]就是對應的函數(shù)名
      break
  if func: #如果路徑內(nèi)容存在函數(shù)就存在
    return func(environ) #執(zhí)行這個函數(shù)
  else:
    print("ok5")
    return [b"404"] #如果不存在就返回404
if __name__=='__main__':
  print("ok0")
  t=make_server("",9700,application)
  print("ok22")
  t.serve_forever()

urls.py文件代碼:

from views import *
URLpattern = (
  ("/login", login),
  ("/alex", foo1),
  ("/egon", foo2),
  ("/auth", auth)
)

views.py文件代碼:

def foo1(request): # 定義函數(shù)
  f=open("templates/alex.html","rb") #打開html 以二進制的模式
  data=f.read() #讀到data里
  f.close() #關閉
  return [data] #返回這個data
def foo2(request):
  f=open("templates/egon.html","rb")
  data=f.read()
  f.close()
  return [data]
def login(request):
  f=open("templates/login.html","rb")
  data=f.read()
  f.close()
  return [data]
def auth(request):
  print("+++",request)
  user_union,pwd_union=request.get("QUERY_STRING").split("&")
  _,user=user_union.split("=")
  _,pwd=pwd_union.split("=")
  if user=='Yuan' and pwd=="123":
    return [b"login,welcome"]
  else:
    return [b"user or pwd is wriong"]

templates目錄下的html文件:

alex.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Title</title>
</head>
<body>
<div>alex</div>
</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<h2>登錄頁面</h2>
<form action="http://127.0.0.1:9700/auth">
  <p>姓名:<input type="text" name="user"></p>
  <p>密碼:<input type="password" name="pwd"></p>
  <p>
    <input type="submit">
  </p>
</form>
</body>
</html>

下面如圖,是目錄結構

訪問ip+prot+路徑 即為相應的html,功能簡單,只是為了熟悉django

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python Socket編程技巧總結》、《Python URL操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論

墨江| 靖州| 同德县| 敦化市| 清镇市| 富平县| 拜泉县| 宣城市| 米易县| 南城县| 临漳县| 阳山县| 疏附县| 玉溪市| 新绛县| 雅安市| 秦皇岛市| 万全县| 香格里拉县| 宜宾市| 依兰县| 本溪市| 元朗区| 泌阳县| 榆中县| 阳朔县| 祥云县| 滦南县| 孝义市| 无为县| 五台县| 祁连县| 筠连县| 涞水县| 宁陕县| 安多县| 玉林市| 辽阳县| 宜宾市| 博客| 三河市|