Flask 学习笔记1 --Qickstart

来源:互联网 发布:紫青双剑仙羽进阶数据 编辑:程序博客网 时间:2024/06/04 18:59

A Basic WSGI Introduction

Werkzeug is a utility library for WSGI(Web Server GateWay Interface). WSGI itself is a protocol or convention that ensures that your web application can speak with the webserver and more importantly that web applications work nicely together.

1. A basic “Hello World” application in WSGI without the help of Werkzeug:



2. SAE Plateform Flask application:




3. Here is how you would write that application with response objects:



Eager to get started? 

 we don't use:http://127.0.0.1:5000/

Our URL is:http://simonrestful.applinzi.com:80/

We use the route() decorator to tell Flask what URL should trigger our function.

To stop the server, hit control-C. However, the server is free!

4. To add variable parts to a URL you can mark these special sections as <variable_name>. Such a part is then passed as a keyword argument to your function.you alse can use <converter:variable_name>

The following converters exist:

intaccepts integersfloatlike int but for floating point valuespathlike the default but also accepts slashes




5. To build a URL to a specific function you can use the url_for() function. It accepts the name of the function as first argument and a number of keyword arguments.

There are three good reasons for this:

  1. Reversing is often more descriptive than hard-coding the URLs. More importantly, it allows you to change URLs in one go, without having to remember to change URLs all over the place.
  2. URL building will handle escaping of special characters and Unicode data transparently for you, so you don’t have to deal with them.
  3. If your application is placed outside the URL root (say, in /myapplication instead of/), url_for() will handle that properly for you.



6. HTTP (the protocol web applications are speaking) knows different methods for accessing URLs.

(1)从网页访问使用的是GET方法;



(2)使用curl命令,以POST方式访问



(3)Python 客户端,以POST方式访问



7. The HTTP method (also often called “the verb(动作)”) tells the server what the clients wants to do with the requested page. The following methods are very common:

(1) GET : The browser tells the server to just get the information stored on that page and send it.

(2) HEAD : The browser tells the server to get the information, but it is only interested in theheaders, not the content of the page.

(3) POST : The browser tells the server that it wants to post some new information to that URL and that the server must ensure the data is stored and only stored once. 

(4) PUT : Similar to POST but the server might trigger the store procedure multiple times by overwriting the old values more than once.(预防掉线)

(5) DELETE : Remove the information at the given location.

(6) OPTIONS : Provides a quick way for a client to figure out which methods are supported by this URL. Starting with Flask 0.6, this is implemented for you automatically.

8. Dynamic web applications also need static files.Flask can do that as well. Just create a folder called staticin your package or next to your module and it will be available at /static on the application.

To generate URLs for static files, use the special 'static' endpoint name:(从错误消息里看prex)




8. Accessing Request Data

For web applications it’s crucial to react to the data a client sent to the server. In Flask this information is provided by the global request object. 

The current request method is available by using the method attribute. To access form data (data transmitted in a POST or PUT request) you can use the form attribute. 



9. To access parameters submitted in the URL (?key=value) you can use the argsattribute

searchword = request.args.get('key', '')  #something wrong?
10. File Upload

You can handle uploaded files with Flask easily. Uploaded files are stored in memory or at a temporary location on the filesystem.  You can access those files by looking at the files attribute on the request object.

Each uploaded file is stored in that dictionary. It behaves just like a standard Python fileobject, but it also has a save() method that allows you to store that file on the filesystem of the server. 


用法?

11. To redirect a user to another endpoint, use the redirect() function.


0 0
原创粉丝点击