日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

02-web框架

發(fā)布時間:2023/11/30 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 02-web框架 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1

while True:print('server is waiting...')conn, addr = server.accept()data = conn.recv(1024) print('data:', data)# 1.得到請求的url路徑# ------------dict/obj d=["path":"/login"]# d.get(”path“)# 按著http請求協(xié)議解析數(shù)據(jù)# 專注于web業(yè)務(wù)開發(fā)path = d.get('path')if path == "/login":return login.html# 按著http響應(yīng)協(xié)議封裝數(shù)據(jù)

?

2

# 報錯信息 C:\Windows\system32>pip install wsgiref Collecting wsgirefUsing cached https://files.pythonhosted.org/packages/41/9e/309259ce8dff8c596e8c26df86dbc4e848b9249fd36797fd60be456f03fc/wsgiref-0.1.2.zipComplete output from command python setup.py egg_info:Traceback (most recent call last):File "<string>", line 1, in <module>File "C:\Users\Venicid\AppData\Local\Temp\pip-build-f8zmzqr9\wsgiref\setup.py", line 5, in <module>import ez_setupFile "C:\Users\Venicid\AppData\Local\Temp\pip-build-f8zmzqr9\wsgiref\ez_setup\__init__.py", line 170print "Setuptools version",version,"or greater has been installed."^SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Setuptools version",version,"or greater has been installed.")?---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in C:\Users\Venicid\AppData\Local\Temp\pip-build-f8zmzqr9\wsgiref\

?

  

# wsgiref 是標(biāo)準(zhǔn)庫,不需要安裝C:\Windows\system32>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC >>> import wsgiref

  

?

from wsgiref.simple_server import make_serverdef application(environ,start_response):# 按著http協(xié)議解析數(shù)據(jù):environ# 按著http協(xié)議組裝數(shù)據(jù):start_responseprint(environ)print(type(environ))start_response('200 OK', [])return [b"<h3>hello wsgiref</h3>"]# 1.封裝socket httpd = make_server("",8802,application)# 2.等待用戶連接: conn,addr = server.accept() httpd.serve_forever() # application(environ,start_response)

?

?

?

?

?根據(jù)path進(jìn)行響應(yīng)不同的html

?

方案1:

from wsgiref.simple_server import make_serverdef application(environ,start_response):# 按著http協(xié)議解析數(shù)據(jù):environ# 按著http協(xié)議組裝數(shù)據(jù):start_responseprint(environ)print(type(environ))# 當(dāng)前的請求路徑path = environ.get('PATH_INFO')print(path)start_response('200 OK', [])if path == "/login":with open('login.html','r') as f:data = f.read()elif path == '/index':with open('index.html','r') as f:data = f.read()elif path == '/favicon.ico': # icon圖標(biāo)with open('favicon.ico','rb') as f:data = f.read()return [data]return [data.encode('utf8')]# 1.封裝socket httpd = make_server("",8802,application)# 2.等待用戶連接: conn,addr = server.accept() httpd.serve_forever() # application(environ,start_response)

?

?方案2:解耦

from wsgiref.simple_server import make_serverdef login():with open('login.html', 'rb') as f:data = f.read()return datadef index():with open('index.html', 'rb') as f:data = f.read()return datadef favi():with open('favicon.ico', 'rb') as f:data = f.read()return datadef application(environ, start_response):# 按著http協(xié)議解析數(shù)據(jù):environ# 按著http協(xié)議組裝數(shù)據(jù):start_responseprint(environ)print(type(environ))# 當(dāng)前的請求路徑path = environ.get('PATH_INFO')start_response('200 OK', [])url_patterns = [("/login",login),('/index',index),('/favicon.ico',favi)]func = Nonefor item in url_patterns:if path == item[0]:func = item[1]breakif func:return [func()]else:return [b'404']# 1.封裝socket httpd = make_server("", 8805, application)# 2.等待用戶連接: conn,addr = server.accept() httpd.serve_forever() # application(environ,start_response)

?

?

?

添加新的reg模塊:快速方便

?

?

?

?

?

?

?

?

  添加請求environ

from wsgiref.simple_server import make_serverdef login(environ):with open('login.html', 'rb') as f:data = f.read()return datadef index(environ):with open('index.html', 'rb') as f:data = f.read()return datadef reg(environ):with open('reg.html', 'rb') as f:data = f.read()return datadef favi(environ):with open('favicon.ico', 'rb') as f:data = f.read()return datadef application(environ, start_response):# 按著http協(xié)議解析數(shù)據(jù):environ# 按著http協(xié)議組裝數(shù)據(jù):start_responseprint(environ)print(type(environ))# 當(dāng)前的請求路徑path = environ.get('PATH_INFO')start_response('200 OK', [])url_patterns = [("/login",login),('/index',index),('/favicon.ico',favi),('/reg',reg)]func = Nonefor item in url_patterns:if path == item[0]:func = item[1]breakif func:return [func(environ)]else:return [b'404']# 1.封裝socket httpd = make_server("", 8805, application)# 2.等待用戶連接: conn,addr = server.accept() httpd.serve_forever() # application(environ,start_response)

?

?

?

?

3、yun框架

?

main.py 啟動程序

from wsgiref.simple_server import make_serverdef application(environ, start_response):path = environ.get('PATH_INFO')start_response('200 OK', [])from urls import url_patterns # 導(dǎo)入路由表 func = Nonefor item in url_patterns:if path == item[0]:func = item[1]breakif func:result = func(environ) # 執(zhí)行視圖函數(shù)return [result]else:return [b'404']print('start yun框架...')# 1.封裝socket httpd = make_server("", 8806, application)# 2.等待用戶連接: conn,addr = server.accept() httpd.serve_forever() # application(environ,start_response)

?

?

urls.py路由分發(fā)

from views import *url_patterns = [("/login", login), # 視圖函數(shù)('/index', index),('/favicon.ico', fav),('/reg', reg),('/timer', timer) ]

?

?

views.py 視圖函數(shù)

def login(environ):with open('./templates/login.html', 'rb') as f:data = f.read()return datadef index(environ):with open('./templates/index.html', 'rb') as f:data = f.read()return datadef reg(environ):with open('./templates/reg.html', 'rb') as f:data = f.read()return datadef fav(environ):with open('./templates/favicon.ico', 'rb') as f:data = f.read()return datadef timer(environ):import datetimenow_time = datetime.datetime.now().strftime('%y-%m-%d %X')return now_time.encode('utf8') # bytes類型

?

?

?

4、

新建 models.py 數(shù)據(jù)交換py文件

# 數(shù)據(jù)庫交互文件# 生成用戶表 import pymysql# 連接數(shù)據(jù)庫 conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='root',db='db61',charset='utf8' )# 獲取游標(biāo) cursor = conn.cursor()# 執(zhí)行sql語句 sql = """ create table userinfo(id int primary key,name varchar(32),password varchar(32) ) """ cursor.execute(sql)# 提交 conn.commit()# 關(guān)閉 cursor.close() conn.close()

?

?

插入一個username ?password

?

views.py中的auth函數(shù)

def auth(request):from urllib.parse import parse_qs # 分割取參數(shù)try:request_body_size = int(request.get('CONTENT_LENGTH',0))except (ValueError):request_body_size = 0request_body = request['wsgi.input'].read(request_body_size)data = parse_qs(request_body)user = data.get(b'username')[0].decode('utf8')pwd = data.get(b'password')[0].decode('utf8')# 連接數(shù)據(jù)庫import pymysqlconn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='root',db='db61',charset='utf8')# 創(chuàng)建游標(biāo)cursor = conn.cursor()# 執(zhí)行sqlsql = "select * from userinfo where name='%s' and password='%s'" % (user, pwd)cursor.execute(sql)if cursor.fetchone(): # 執(zhí)行成功,返回一條信息result = index(request) # 登錄成功,進(jìn)入index頁面return resultelse:return 'Error username or passwrod'.encode('utf8')

?

?

?

?

?

5、總結(jié)

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/venicid/p/9231549.html

總結(jié)

以上是生活随笔為你收集整理的02-web框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。