2020-09-26

Django——web框架(基于wsgiref自己写)

一、web框架

web框架就是在以上十几行代码基础张扩展出来的,有很多简单方便使用的方法,大大提高了开发的效率

二、基于wsgiref写一个web框架

 

 

 models.py

'''链接数据库返回字典格式:cursor=pymysql.cursors.DictCursor'''import pymysql#连接数据库conn = pymysql.connect(host='127.0.0.1',port= 3306,user = 'root',passwd='123456',db='web') #db:库名#创建游标cur = conn.cursor()sql='''create table userinfo(  id INT PRIMARY KEY ,  name VARCHAR(32) ,  password VARCHAR(32))'''cur.execute(sql)#提交conn.commit()#关闭指针对象cur.close()#关闭连接对象conn.close()
models.py

myserver.py

from wsgiref.simple_server import make_serverfrom urls import url_pattersdef server(environ, start_response): # 必须传两个参数 start_response('200 OK', [('Content-Type', 'text/html')]) func=None for item in url_patters:  if item[0] == environ.get('PATH_INFO'):   func = item[1]   break if func:  data = func(environ, start_response) else:  data='error'.encode('utf-8') return [data]if __name__ == '__main__': myserver = make_server('127.0.0.1', 8081, server) myserver.serve_forever()
myserver.py

urls.py

# 路径和函数对应关系from views import *url_patters = [ ('/login', login), ('/index', index), ('/time', time), ('/order', order),]
urls.py

views.py

def login(environ, start_response): with open('templates/login.html', 'rb') as f:  data = f.read() return datadef index(environ, start_response): with open('templates/index.html', 'rb') as f:  data = f.read() return datadef time(environ, start_response): import time with open('templates/time.html', 'r', encoding='utf-8') as f:  #    replace(替换前 ,替换后)  data = f.read().replace('{{time}}', str(time.time())) return data.encode('utf-8')def order(environ, start_response): return 'order'.encode('utf-8')
views.py

tempaltes下backend.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body>登录成功</body></html>
tempaltes下backend.html

tempaltes下index.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body>index</body></html>
tempaltes下index.html

tempaltes下login.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><h4>登录页面</h4><form action="" method="post">  用户名 <input type="text" name="user">  密码 <input type="text" name="pwd"> <input type="submit"></form></body></html>
tempaltes下login.html

其它socket服务端

'cgi': CGIServer, 'flup': FlupFCGIServer, 'wsgiref': WSGIRefServer, 'waitress': WaitressServer, 'cherrypy': CherryPyServer, 'paste': PasteServer, 'fapws3': FapwsServer, 'tornado': TornadoServer, 'gae': AppEngineServer, 'twisted': TwistedServer, 'diesel': DieselServer, 'meinheld': MeinheldServer, 'gunicorn': GunicornServer, 'eventlet': EventletServer, 'gevent': GeventServer, 'geventSocketIO':GeventSocketIOServer, 'rocket': RocketServer, 'bjoern' : BjoernServer, 'auto': AutoServer,
其它socket服务端

 

原文转载:http://www.shaoqun.com/a/477668.html

跨境通电子商务平台:https://www.ikjzd.com/w/1329.html

c79:https://www.ikjzd.com/w/1016

泛亚班拿:https://www.ikjzd.com/w/1262


一、web框架web框架就是在以上十几行代码基础张扩展出来的,有很多简单方便使用的方法,大大提高了开发的效率二、基于wsgiref写一个web框架models.py'''链接数据库返回字典格式:cursor=pymysql.cursors.DictCursor'''importpymysql#连接数据库conn=pymysql.connect(hos
海淘贝:https://www.ikjzd.com/w/1726
巴克莱银行:https://www.ikjzd.com/w/2775
YinoLink易诺_关于易诺:https://www.ikjzd.com/w/2666
独立站到底Shopify 还是Woocommerce ?:https://www.ikjzd.com/home/97731
月销十万冲百万 亚马逊卖家有哪些命门要过:https://www.ikjzd.com/home/17521

No comments:

Post a Comment