中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

模板做的網(wǎng)站不好優(yōu)化手機(jī)網(wǎng)站模板免費(fèi)下載

模板做的網(wǎng)站不好優(yōu)化,手機(jī)網(wǎng)站模板免費(fèi)下載,網(wǎng)站程序設(shè)計(jì)軟件,旅游公網(wǎng)站如何做web框架: 使用web框架專門負(fù)責(zé)處理用戶的動(dòng)態(tài)資源請(qǐng)求,這個(gè)web框架其實(shí)就是一個(gè)為web服務(wù)器提供服務(wù)的應(yīng)用程序 什么是路由? 路由就是請(qǐng)求的url到處理函數(shù)的映射,也就是說提前把請(qǐng)求的URL和處理函數(shù)關(guān)聯(lián)好 管理路由可以使用一個(gè)…

web框架:

使用web框架專門負(fù)責(zé)處理用戶的動(dòng)態(tài)資源請(qǐng)求,這個(gè)web框架其實(shí)就是一個(gè)為web服務(wù)器提供服務(wù)的應(yīng)用程序

什么是路由?

路由就是請(qǐng)求的url到處理函數(shù)的映射,也就是說提前把請(qǐng)求的URL和處理函數(shù)關(guān)聯(lián)好

管理路由可以使用一個(gè)路由列表進(jìn)行管理

web.py文件:

importsocketimportosimportthreadingimportsysimportframework#http協(xié)議的web服務(wù)器類classHttpWebServer(object):def__init__(self,port):#創(chuàng)建tcp服務(wù)端套接字tcp_server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#設(shè)置端口號(hào)復(fù)用,程序退出端口號(hào)立即釋放tcp_server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)#綁定端口號(hào)tcp_server_socket.bind(("",port))#設(shè)置監(jiān)聽tcp_server_socket.listen(128)#把tcp服務(wù)器的套接字作為web服務(wù)器對(duì)象的屬性self.tcp_server_socket=tcp_server_socket#處理客戶端請(qǐng)求@staticmethoddefhandle_client_request(new_socket):#接收客戶端的請(qǐng)求信息recv_data=new_socket.recv(4096)#判斷接收的數(shù)據(jù)長(zhǎng)度是否為0iflen(recv_data)==0:new_socket.close()return#對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行解碼recv_content=recv_data.decode("utf-8")print(recv_content)#對(duì)數(shù)據(jù)按照空格進(jìn)行分割request_list=recv_content.split("",maxsplit=2)#獲取請(qǐng)求的資源路徑request_path=request_list[1]print(request_path)#判斷請(qǐng)求的是否是根目錄,如果是根目錄設(shè)置返回的信息ifrequest_path=="/":request_path="/index.html"#判斷是否是動(dòng)態(tài)資源請(qǐng)求,以后把后綴是.html的請(qǐng)求任務(wù)是動(dòng)態(tài)資源請(qǐng)求ifrequest_path.endswith(".html"):"""動(dòng)態(tài)資源請(qǐng)求"""#動(dòng)態(tài)資源請(qǐng)求找web框架進(jìn)行處理,需要把請(qǐng)求參數(shù)給web框架#準(zhǔn)備給web框架的參數(shù)信息,都要放到字典里面env={"request_path":request_path,#傳入請(qǐng)求頭信息,額外的參數(shù)可以在字典里面在進(jìn)行添加}#使用框架處理動(dòng)態(tài)資源請(qǐng)求,#1.web框架需要把處理結(jié)果返回給web服務(wù)器,#2.web服務(wù)器負(fù)責(zé)把返回的結(jié)果封裝成響應(yīng)報(bào)文發(fā)送給瀏覽器status,headers,response_body=framework.handle_request(env)print(status,headers,response_body)#響應(yīng)行response_line="HTTP/1.1%s\r\n"%status#響應(yīng)頭response_header=""forheaderinheaders:response_header+="%s:%s\r\n"%header#響應(yīng)報(bào)文response_data=(response_line+response_header+"\r\n"+response_body).encode("utf-8")#發(fā)送響應(yīng)報(bào)文數(shù)據(jù)給瀏覽器new_socket.send(response_data)#關(guān)閉連接new_socket.close()else:"""靜態(tài)資源請(qǐng)求"""#1.os.path.exits#os.path.exists("static/"+request_path)#2.try-excepttry:#打開文件讀取文件中的數(shù)據(jù),提示:這里使用rb模式,兼容打開圖片文件withopen("static"+request_path,"rb")asfile:#這里的file表示打開文件的對(duì)象file_data=file.read()#提示:withopen關(guān)閉文件這步操作不用程序員來完成,系統(tǒng)幫我們來完成exceptExceptionase:#代碼執(zhí)行到此,說明沒有請(qǐng)求的該文件,返回404狀態(tài)信息#響應(yīng)行response_line="HTTP/1.1404NotFound\r\n"#響應(yīng)頭response_header="Server:PWS/1.0\r\n"#讀取404頁面數(shù)據(jù)withopen("static/error.html","rb")asfile:file_data=file.read()#響應(yīng)體response_body=file_data#把數(shù)據(jù)封裝成http響應(yīng)報(bào)文格式的數(shù)據(jù)response=(response_line+response_header+"\r\n").encode("utf-8")+response_body#發(fā)送給瀏覽器的響應(yīng)報(bào)文數(shù)據(jù)new_socket.send(response)else:#代碼執(zhí)行到此,說明文件存在,返回200狀態(tài)信息#響應(yīng)行response_line="HTTP/1.1200OK\r\n"#響應(yīng)頭response_header="Server:PWS/1.0\r\n"#響應(yīng)體response_body=file_data#把數(shù)據(jù)封裝成http響應(yīng)報(bào)文格式的數(shù)據(jù)response=(response_line+response_header+"\r\n").encode("utf-8")+response_body#發(fā)送給瀏覽器的響應(yīng)報(bào)文數(shù)據(jù)new_socket.send(response)finally:#關(guān)閉服務(wù)于客戶端的套接字new_socket.close()#啟動(dòng)服務(wù)器的方法defstart(self):#循環(huán)等待接受客戶端的連接請(qǐng)求whileTrue:#等待接受客戶端的連接請(qǐng)求new_socket,ip_port=self.tcp_server_socket.accept()#代碼執(zhí)行到此,說明連接建立成功sub_thread=threading.Thread(target=self.handle_client_request,args=(new_socket,))#設(shè)置成為守護(hù)主線程sub_thread.setDaemon(True)#啟動(dòng)子線程執(zhí)行對(duì)應(yīng)的任務(wù)sub_thread.start()defmain():##獲取終端命令行參數(shù)#params=sys.argv#iflen(params)!=2:#print("執(zhí)行的命令格式如下:python3xxx.py9000")#return#####判斷第二個(gè)參數(shù)是否都是由數(shù)字組成的字符串#ifnotparams[1].isdigit():#print("執(zhí)行的命令格式如下:python3xxx.py9000")#return#####代碼執(zhí)行到此,說明命令行參數(shù)的個(gè)數(shù)一定2個(gè)并且第二個(gè)參數(shù)是由數(shù)字組成的字符串#port=int(params[1])#創(chuàng)建web服務(wù)器web_server=HttpWebServer(8000)#啟動(dòng)服務(wù)器web_server.start()#判斷是否是主模塊的代碼if__name__=='__main__':main()framework.py文件:"""web框架的職責(zé)專門負(fù)責(zé)處理動(dòng)態(tài)資源請(qǐng)求"""importtimeroute_list=[]#定義帶有參數(shù)的裝飾器defroute(path):defdecorator(func):#裝飾器執(zhí)行的時(shí)候就需要把路由添加到路由列表里route_list.append((path,func))definner():result=func();returnresultreturninnerreturndecorator#獲取首頁數(shù)據(jù)@route("/index.html")defindex():#狀態(tài)信息status="200OK"#響應(yīng)頭信息response_header=[("Server","PWS/1.1")]#1.打開指定模板文件,讀取模板文件中的數(shù)據(jù)withopen("template/index.html","r",encoding='utf-8')asfile:file_data=file.read()#2.查詢數(shù)據(jù)庫,模板里面的模板變量替換成以后從數(shù)據(jù)庫里查詢的數(shù)據(jù)#web框架處理后的數(shù)據(jù)#獲取當(dāng)前時(shí)間,模擬數(shù)據(jù)庫內(nèi)容data=time.ctime()response_body=file_data.replace("{%content%}",data)#這里返回的是元組returnstatus,response_header,response_body#獲取個(gè)人中心數(shù)據(jù)@route("/center.html")defcenter():#狀態(tài)信息status="200OK"#響應(yīng)頭信息response_header=[("Server","PWS/1.1")]#1.打開指定模板文件,讀取模板文件中的數(shù)據(jù)withopen("template/center.html","r",encoding='utf-8')asfile:file_data=file.read()#2.查詢數(shù)據(jù)庫,模板里面的模板變量替換成以后從數(shù)據(jù)庫里查詢的數(shù)據(jù)#web框架處理后的數(shù)據(jù)#獲取當(dāng)前時(shí)間,模擬數(shù)據(jù)庫內(nèi)容data=time.ctime()response_body=file_data.replace("{%content%}",data)#這里返回的是元組returnstatus,response_header,response_body#處理沒有找到的動(dòng)態(tài)資源defnot_found():#狀態(tài)信息status="404NotFound"#響應(yīng)頭信息response_header=[("Server","PWS/1.1")]#web框架處理后的數(shù)據(jù)data="notfound"#這里返回的是元組returnstatus,response_header,data#處理動(dòng)態(tài)資源請(qǐng)求defhandle_request(env):#獲取動(dòng)態(tài)的請(qǐng)求資源路徑request_path=env["request_path"]print("動(dòng)態(tài)資源請(qǐng)求的地址:",request_path)#判斷請(qǐng)求的動(dòng)態(tài)資源路徑,選擇指定的函數(shù)處理對(duì)應(yīng)的動(dòng)態(tài)資源請(qǐng)求forpath,funcinroute_list:ifrequest_path==path:result=func()returnresultelse:result=not_found()returnresult#ifrequest_path=="/index.html":##獲取首頁數(shù)據(jù)#result=index()##把處理后的結(jié)果返回給web服務(wù)器使用,讓web服務(wù)器拼接響應(yīng)報(bào)文時(shí)使用#returnresult#elifrequest_path=="/center.html":##個(gè)人中心#result=center()#returnresult#else:##沒有動(dòng)態(tài)資源數(shù)據(jù),返回404狀態(tài)信息#result=not_found()##把處理后的結(jié)果返回給web服務(wù)器使用,讓web服務(wù)器拼接響應(yīng)報(bào)文時(shí)使用#returnresultif__name__=="__main__":print(route_list)

framework.py文件:

"""web框架的職責(zé)專門負(fù)責(zé)處理動(dòng)態(tài)資源請(qǐng)求"""
import time
route_list=[]
#定義帶有參數(shù)的裝飾器
def route(path):def decorator(func):#裝飾器執(zhí)行的時(shí)候就需要把路由添加到路由列表里route_list.append((path, func))def inner():result=func();return resultreturn innerreturn decorator# 獲取首頁數(shù)據(jù)
@route("/index.html")
def index():# 狀態(tài)信息status = "200 OK"# 響應(yīng)頭信息response_header = [("Server", "PWS/1.1")]# 1.打開指定模板文件,讀取模板文件中的數(shù)據(jù)with open("template/index.html","r",encoding='utf-8') as file:file_data=file.read()# 2.查詢數(shù)據(jù)庫,模板里面的模板變量替換成以后從數(shù)據(jù)庫里查詢的數(shù)據(jù)# web框架處理后的數(shù)據(jù)# 獲取當(dāng)前時(shí)間,模擬數(shù)據(jù)庫內(nèi)容data = time.ctime()response_body=file_data.replace("{%content%}",data)# 這里返回的是元組return status, response_header, response_body#獲取個(gè)人中心數(shù)據(jù)
@route("/center.html")
def center():# 狀態(tài)信息status = "200 OK"# 響應(yīng)頭信息response_header = [("Server", "PWS/1.1")]# 1.打開指定模板文件,讀取模板文件中的數(shù)據(jù)with open("template/center.html","r",encoding='utf-8') as file:file_data=file.read()# 2.查詢數(shù)據(jù)庫,模板里面的模板變量替換成以后從數(shù)據(jù)庫里查詢的數(shù)據(jù)# web框架處理后的數(shù)據(jù)# 獲取當(dāng)前時(shí)間,模擬數(shù)據(jù)庫內(nèi)容data = time.ctime()response_body=file_data.replace("{%content%}",data)# 這里返回的是元組return status, response_header, response_body# 處理沒有找到的動(dòng)態(tài)資源
def not_found():# 狀態(tài)信息status = "404 Not Found"# 響應(yīng)頭信息response_header = [("Server", "PWS/1.1")]# web框架處理后的數(shù)據(jù)data = "not found"# 這里返回的是元組return status, response_header, data# 處理動(dòng)態(tài)資源請(qǐng)求
def handle_request(env):# 獲取動(dòng)態(tài)的請(qǐng)求資源路徑request_path = env["request_path"]print("動(dòng)態(tài)資源請(qǐng)求的地址:", request_path)# 判斷請(qǐng)求的動(dòng)態(tài)資源路徑,選擇指定的函數(shù)處理對(duì)應(yīng)的動(dòng)態(tài)資源請(qǐng)求for path, func in route_list:if request_path == path:result = func()return resultelse:result = not_found()return result# if request_path == "/index.html":#     # 獲取首頁數(shù)據(jù)#     result = index()#     # 把處理后的結(jié)果返回給web服務(wù)器使用,讓web服務(wù)器拼接響應(yīng)報(bào)文時(shí)使用#     return result# elif request_path=="/center.html":#    #個(gè)人中心#     result=center()#     return result# else:#     # 沒有動(dòng)態(tài)資源數(shù)據(jù), 返回404狀態(tài)信息#     result = not_found()#     # 把處理后的結(jié)果返回給web服務(wù)器使用,讓web服務(wù)器拼接響應(yīng)報(bào)文時(shí)使用#     return result
if __name__=="__main__":print(route_list)

http://www.risenshineclean.com/news/30809.html

相關(guān)文章:

  • 良鄉(xiāng)網(wǎng)站建設(shè)百度應(yīng)用
  • 經(jīng)營網(wǎng)站需要什么資質(zhì)網(wǎng)站推廣在哪好
  • 自己做新聞網(wǎng)站推廣衣服的軟文
  • vs2017 如何做網(wǎng)站網(wǎng)絡(luò)營銷課程主要講什么內(nèi)容
  • 贛州市微程網(wǎng)絡(luò)科技有限公司徐州自動(dòng)seo
  • 網(wǎng)站業(yè)務(wù)費(fèi)如何做記賬憑證優(yōu)化分析
  • 做網(wǎng)站做推廣有效果嗎做廣告推廣哪個(gè)平臺(tái)好
  • 企業(yè)官網(wǎng)建站流程免費(fèi)建站的網(wǎng)站哪個(gè)好
  • 自助免費(fèi)建站系統(tǒng)電池優(yōu)化大師下載
  • 河北保定疫情最新消息今天封城了seo權(quán)重是什么意思
  • 如何用電腦做網(wǎng)站服務(wù)器市場(chǎng)seo是什么意思
  • 政府網(wǎng)站建設(shè)公司 政務(wù)qq推廣引流網(wǎng)站
  • 境外公司注冊(cè)優(yōu)化工具箱下載
  • 做圖網(wǎng)站有哪些線上網(wǎng)絡(luò)推廣怎么做
  • 導(dǎo)航欄網(wǎng)站模板特效搭建一個(gè)網(wǎng)站的流程
  • 中國企業(yè)培訓(xùn)網(wǎng)蘭州seo培訓(xùn)
  • 如何建設(shè)網(wǎng)站山東濟(jì)南興田德潤官網(wǎng)域名檢測(cè)工具
  • 俄語網(wǎng)站建設(shè)公司沒經(jīng)驗(yàn)可以做電商運(yùn)營嗎
  • 模仿京東商城網(wǎng)站開發(fā)視頻蘋果自研搜索引擎或?yàn)樘娲雀?/a>
  • 免抵退稅在哪個(gè)網(wǎng)站做北京新聞最新消息
  • 電商網(wǎng)站建設(shè)與運(yùn)營seo專員工作容易學(xué)嗎
  • 順德新網(wǎng)站制作行業(yè)關(guān)鍵詞一覽表
  • 個(gè)人網(wǎng)站如何做專業(yè)的營銷團(tuán)隊(duì)哪里找
  • 廣州做網(wǎng)站哪家好網(wǎng)站推廣怎么做有效果
  • 網(wǎng)站公安備案網(wǎng)址電商平臺(tái)怎么加入
  • 凡科免費(fèi)建設(shè)企業(yè)網(wǎng)站靠譜嗎個(gè)人做外貿(mào)怎樣起步
  • 電子商務(wù)網(wǎng)站建設(shè)花費(fèi)今日新聞?lì)^條新聞最新
  • 泗陽城鄉(xiāng)建設(shè)局網(wǎng)站域名seo查詢
  • 百度優(yōu)化網(wǎng)站建設(shè)wordpress外貿(mào)獨(dú)立站
  • 網(wǎng)站維護(hù)需要會(huì)什么海外免費(fèi)網(wǎng)站推廣