A: The nature of the Web framework, for all Web applications, is essentially a socket server, the User's browser is actually a socket Client.
1.1:python implementation:
#!/usr/bin/env python#coding:utf-8import socketdef handle_request (client): buf = client.recv (1024x768) Client.send ("http/1.1 Ok\r\n\r\n") client.send ("Hello, Seven") def main (): sock = Socket.socket (socket.af _inet, Socket. Sock_stream) sock.bind ((' localhost ', 8000)) sock.listen (5) while True: connection, address = Sock.accept () handle_request (connection) connection.close () if __name__ = = ' __main__ ':
1.2: Implementation Method:
Implement Socket,tornado Yourself
Based on the Wsgi implementation, Django
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,}
1.3:wsgi (web Server Gateway Interface) is a specification that defines the interface format between Web apps and Web servers written in python, enabling decoupling between Web apps and Web Servers.
The standalone WSGI server provided by the Python standard library is called wsgiref.
#!/usr/bin/env python#coding: Utf-8 FromWsgiref.Simple_serverImportMake_serverDef Runserver(Environ,Start_response):Start_response(' OK ', [(' Content-type ', ' Text/html ')]) Return ' #路由系统根据用户请求的不同URL返回不同的页面 if __name__ == ' __main__ ' : httpd Span class= "pun" >= Make_server ( ", 8000, runserver print "serving HTTP on Port 8000 ... " Httpd. ()
#执行结果:
1.4: Customizing a Web Framework:
#!/usr/bin/env python#coding:utf-8from wsgiref.simple_server Import make_server def index (): return ' index ' def Login (): return ' login ' def routers (): urlpatterns = ( ('/index/', index), ('/login/', login), ) return urlpatterns def runserver (environ, start_response): start_response (' ok ', [' content-type ', ' text/ HTML ')]) url = environ[' path_info '] urlpatterns = Routers () func = None for item in Urlpatterns: if item[0] = = Url: func = item[1] break if Func: return func () else: return ' 404 Not found ' if __name__ = = ' __main__ ': httpd = make_server (", 8000, runserver) print" serving HTTP on port 8000 ... " httpd.serve_forever ()
The difference between 1.4:MVC and mtv:
Mvc:models: manipulating database Views: placing HTML templates controllers handling user requests Mtv:models: manipulating database related temalate: placing HTML templates viers processing user requests, Django is this pattern
Two: implementing a web framework through Python
2.1: Install Python:
#pip3 Install the Django #这是python 3.5.1 version
2.2: Set Environment Variables:
2.3: Create Django Project:
2.3.1# command line mode: django-admin startproject MySite
2.3.2# Graphics mode: file-new project-django-create-new window opens
3: Create and start App,app share the data in project, the app is used to respond to user requests, and each type of website is an app, equivalent to a business:
3.1: go to Project directory, execute command python3 manage.py Startapp CMDB launch create an app
3:1.2: write a route map, write a function, as long as the URL conforms to the rules to execute the function, the function return a value, this is a complete URL request
3.1.3: Configuring the Index function
3.1.4: Launch App:
#命令启动: python manage.py runserver 127.0.0.1:8000
#图形启动1: first configuration, upper right Corner-edit configrations:
#图形启动2, Click Run:
3.1.5: Boot information:
3.1.6: Visit page:
3.2: Configure the HTML file to return to the user, HTML in the MTV architecture to be placed in the templates directory:
3.2.1: defines an HTML file:
3.2.2: Configure the return Web Page:
3.2.3: Test Access:
3.2.4: define the template path using the settings configuration file:
Base_dir=Os.Path.DirName(Os.Path.DirName(Os.Path.Abspath(__file__))) #获取到主目录Mysite_djangoThe absolute pathTEMPLATES= [ { ' Backend ': ' Django.template.backends.django.DjangoTemplates ', ' DIRS ': [Os.Path.Join(Base_dir, ' Templates ')] #将TemplatesDirectory is added toBase_dirEnvironment variables, If any other directory is added here to theBase_dirdirectory, confirm that the path can be found , ' App_dirs ': True, "OPTIONS" : { ' context_processors ' : [ ' django.template.context_processors.debug ' , ' django.template.context_processors.request ' , ' Django.contrib.auth.context_processors.auth ' , ' django.contrib.messages.context_processors.messages ' , Span class= "pun" >], }, }, ] /span>
3.2.5: defines the path where the static page is Stored:
3.2.6: Configure HTML references to static files in the statics directory, css, and so on are static files:
3.3: access page Test:
3.3.1:jquery Open Page:
Python growth path 17