Reference link: https: //www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432339008728d0ddbe19ee594980be3f0644a9371894000
Explained: https: //blog.csdn.net/qq_38801354/article/details/73008111
Rewrite the benefits of web frameworks
1) can be directly written
# Process parameters of URL / blog / {id} could write: @get ( '/ Blog / {ID}') DEF get_blog (ID): Pass # query_string process parameters can be designated keyword or key parameters ** kw the parameter receiving word :( still do not understand) @get ( '/ API / Comments') DEF api_comments (*, Page = '. 1'): Pass # for the function return value is not necessarily web.Response object, is str, bytes or dict. # So if you want to render a template, so we can return a dict: return { '__template__': 'index.html', 'the Data': '...' }
And must not require the use of aiohttp when some steps
# Directly aiohttp Import ASYNCIO from Web Import aiohttp the async DEF index (Request): the await asyncio.sleep (0.5) return web.Response (body = '</ h1 of> <h1 of> Index' B) # ------ -------------------------------------------- look at the async def hello ( request): # the async keyword adding coroutine decorator the await asyncio.sleep (0.5) # of operation request parameters, acquisition operation responsive text = '<h1> hello, % s </ h1>!'% request. match_info [ 'name'] # reponse constructed object and return return web.Response (= text.encode body ( 'UTF-. 8')) the async DEF the init (Loop): App = web.Application (Loop = Loop) app.router .add_route ( 'the GET', '/', index) app.router.add_route ( 'the GET', '/hello/{name}', hello) SRV = the await loop.create_server (App.make_handler (), '127.0.0.1', 8000) print('Server started at http://127.0.0.1:8000...') return srv loop = asyncio.get_event_loop() loop.run_until_complete(init(loop)) loop.run_forever()
Therefore, some steps from web framework defined above energy package, so you only need to write a function like
2) can be individually tested module, otherwise, you need to simulate a browser request that is before the request can be achieved
Start writing Web framework
The first step: to frame basis to write aiohttp
@get and @post
Http defines the different methods of interaction with the server, there are four basic methods, namely GET, POST, PUT, DELETE. URL stands for resource descriptors, we can be considered: a URL address that is used to describe resources on a network, and in HTTP GET, POST, PUT, DELETE it corresponds to the investigation of this resource, change, add, 4 puncturing operation. Recommendation: 1 , security get way than the Post the way to badly, contain confidential information, the proposal submission by Post data; 2, when doing data query, recommended Get way; but in doing data add, modify or delete it is recommended by Post manner;
To a function mapping (equivalent to binding) as a URL handler
Step two: Defining RequestHandler
URL handler written user is not necessarily a coroutine, so we use RequestHandler () to encapsulate a URL handler (do not know)
RequestHandler purpose is to analyze their needs to receive arguments from the URL function, obtain the necessary parameters from the request , call the URL functions.
RequestHandler
It is a class when creating the definition of a __call__ () method, so it can be an example as a function.
The role of Call method: https: //www.cnblogs.com/superxuezhazha/p/5793536.html
# A class instance can become a callable object only needs to implement a particular method __call __ (). # We have become a Person class callable: class Person (Object): DEF __init __ (Self, name, Gender): self.name = name self.gender = Gender DEF __call __ (Self, Friend): Print 'My name % S ... IS 'the self.name% Print' My Friend% S ... IS 'Friend% # can now directly call to Person instance: >>> Person P = (' Bob ',' MALE ') #__ init_ _ the incoming parameter is doing such a thing, similar to the initialization >>> p ( 'Tim') # and then use examples to invoke My name iS Bob ... My Friend iS Tim ... # look p ( 'Tim') you can not determine or p is a function of a class instance, therefore, in Python, is the difference between the object, the object function and the function is not significant.