web框架和django基础(粗糙版)

时间:2021-02-27 20:05:03

web框架本质:
    浏览器:socket客户端
    服务器:socket服务端
   
   
1.自己写socket服务端(最傻)
        #!/usr/bin/env python
        # -*-
coding:utf-8 -*-
        import socket

def handle_request(client):
            date =
client.recv(1024)
            client.send(b'HTTP/1.1 200
OK\r\n\r\n')
            client.send(b'Hello')
        def
main():
            sock =
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
           
sock.bind(('localhost',8000))
            sock.listen(5)
            while
True:
                conn,addr = sock.accept()
               
handle_request(conn)
                conn.close()
        if __name__ ==
'__main__':
            main()
    2.次之(web框架的开发者) 
       
WSGI:通用网关服务接口
            server_names = {
                '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,
            }
       
Python中的模块来实现web框架
            from wsgiref.simple_server import
make_server
            def RunServer(environ,
start_response):
                # environ 用户请求的相关信息
                #
start_response  设置用户相应的相关信息
                start_response('200 OK',
[('Content-Type', 'text/html')])
                return
[bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
           
if __name__ == '__main__':
                httpd = make_server('localhost',
8000, RunServer)
                print("Serving HTTP on port
8000...")
                httpd.serve_forever()
               
   
3.现成的web框架 Bottle,Flask,Django,tornado(不是用django写的)

四,Django
    web框架分两类:
    (功能齐全)
        Django       
大而全
        Bottle,Flask,Tornado,Webpy
    安装:
        1.pip3 install
django
        2.解压
            python3 setup.py install
           
可执行文件  django-admin.exe 目录在python3下的script下
           
#添加环境变量
           
        3.基本使用
           
创建project
                django-admin startproject
projectname(Mysite)
               
               
mysite
                    - mysite        (配置文件)
                    -
manage.py        (管理project)
                    -
app(cmdb)
                        - models.py    
数据库操作
                        - admin.py        
配置django自带的后台管理
                        - apps.py        
当前app的配置
                        - test.py       
单元测试
                        - views.py        做业务处理
           

            运行
                cd mysite
                python3
manage.py runserver 127.0.0.1:8000
               
           
创建app(处理业务逻辑)
                cd mysite
                python3 manage.py
startapp cmdb
                python3 manage.py startapp
monitor(监控)
               
        django的基本使用
           
urls中的配置
                from cmdb(appname) import views
               
urlpatterns = [
                    url(r'^admin/',
admin.site.urls),#默认的管理界面
                    url(r'^index/',
views.index),#自己配置的指向views.index
                ]
           
views中的配置
                from django.shortcuts import
HttpResponse
                # 至少一个参数request
                #
request封装用户请求相关信息
                def index(request):
                   
print(request.GET)
                    return HttpResponse('ok')