Fresh in Werkzeug

Source: Internet
Author: User
Tags wrappers

    • Hello World WSGI App
    • WSGI applications
    • WSGI Environment
      • Creating request requests with the Environ parameter
      • Responses
Hello World WSGI App
def application (environ, start_response):    start_response (        ' OK ',        [(' Content-type ', ' Text/plain ')]    )    return [' Hello World ']

The parameters are environ dictionaries and callable start_response

WSGI applications

The WSGI application is an application that we can invoke and then pass a environ dictionary and a start_response callable object.

From werkzeug.wrappers import request, Responsedef application (environ, start_response):    request = Request (environ    text = ' Hello%s! '% request.args.get (' name ', ' World ')    response = response (text, mimetype= ' Text/plain ')    Return response (environ, start_response)
WSGI Environment

The WSGI environment contains all the information that a user sends a request. Use create_environ() a function to create a wsgi environ

From werkzeug.test Import Create_environenviron = Create_environ ('/foo ', ' http://localhost:8080/') >>> environ{' content_length ': ' 0 ', ' content_type ': ', ' http_host ': ' localhost:8080 ', ' path_info ': '/foo ', ' query_string ': ', ' request_method ': ' GET ', ' script_name ': ', ' server_name ': ' localhost ', ' server_port ': ' 8080 ', ' server_protocol ': ' http/1.1 ', ' wsgi.errors ': <open file ' <stderr> ', Mode ' W ' at 0x0000000001d52150>, ' Wsgi.input ': < Cstringio.stringo at 0x34838f0>, ' wsgi.multiprocess ': false, ' wsgi.multithread ': false, ' wsgi.run_once ': false, ' Wsgi.url_scheme ': ' http ', ' wsgi.version ': (1, 0)}

Output can get environ dictionary content, need to pay attention to the content is the request way get,host to localhost:8080, path information/foo, server port 8080, etc.

Creating request requests with the Environ parameter
From werkzeug.wrappers Import requestrequest = Request (environ) >>> request.host ' localhost:8080 ' >>> Request.url ' Http://localhost:8080/foo '

To facilitate testing, we can use functions from_values() to provide some parameters to create a Request object.

>>> from Cstringio import stringio>>> data = "name=this+is+encoded+form+data&another_key= Another+one ">>> request = request.from_values (query_string= ' Foo=bar&blah=blafasel ',...    Content_length=len (data), Input_stream=stringio (data),...    Content_type= ' application/x-www-form-urlencoded ',...    Method= ' post ') ...>>> Request.method ' Post '

request.argsA dictionary of request parameters can be obtained and a dictionary of the data in the request.form request can be obtained.

The file is also very simple to upload, you request.files can get a dictionary of the file

def store_file (Request):    file = Request.files.get (' my_file ')    if file:        file.save ('/where/to/store/ File.txt ')    else:        handle_the_error ()

The file here is a Filestorage object that provides some common file operations

request.headersIs the content of the request header

Can impersonate a browser to make a request

>>> environ = Create_environ () >>> environ.update (...     Http_user_agent= ' mozilla/5.0 (Macintosh; U Mac OS X 10.5; En-us; ) firefox/3.1 ',...     http_accept= ' text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ',...     Http_accept_language= ' de-at,en-us;q=0.8,en;q=0.5 ',...     http_accept_encoding= ' gzip,deflate ',...     http_accept_charset= ' iso-8859-1,utf-8;q=0.7,*;q=0.7 ',...     Http_if_modified_since= ' Fri, 10:10:25 GMT ',...     Http_if_none_match= ' "e51c9-1e5d-46356dc86c640" ',...     Http_cache_control= ' max-age=0 ' ...) ...>>> request = Request (environ)

And then we can get the information through request.user_agent.

>>> request.user_agent.browser ' Firefox ' >>> request.user_agent.platform ' MacOS ' >>> Request.user_agent.version ' 3.1 ' >>> request.user_agent.language ' en-US '

View browser-supported Mimetypes

>>> request.accept_mimetypestext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8>> > request.accept_mimetypes.best ' text/html '

You can use the same method to view supported languages, encodings, and so on.

Responses

The response object is used to send data to the user. The response object is actually a WSGI application, so actually our operation is not to return a response object, but to call the response object and return the resulting value in the WSGI application like a Wsgi object.

For example, our WSGI application is this:

def application (environ, start_response):    start_response (        ' OK ',         [(' Content-type ', ' Text/plain ')]    )    return [' Hello World ']

Then our response object can be as follows:

From werkzeug.wrappers import responsedef application (environ, start_response):    response = response (' Hello World ')    return response (environ, start_response)

Unlike the request object, the response object is designed to be modifiable, and we can directly modify the value of the property:

>>> from werkzeug.wrappers import response>>> Response = Response ("Hello world!") >>> response.headers[' content-type '] ' text/plain; Charset=utf-8 ' >>> response.data ' Hello world! ' >>> response.headers[' content-length ' = Len (response.data) >>> response.status ' OK ' >> > response.status = ' 404 Not Found ' >>> Response.status_code = 400

Cookies can also be set in response.

>>> res = Response (' Hello World ') >>> Res.set_cookie (' name ', ' value ') >>> res.headers[' Set-cookie '] ' name=value; path=/' >>> res.set_cookie (' name1 ', ' value1 ') >>> res.headers.getlist (' Set-cookie ') [' Name=value; path=/', ' name1=value1; path=/']

When setting more than one cookie, you can use it getlist to get a list of cookies.

Fresh in Werkzeug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.