In pure wsgi, your application can return a very limited number of data types, you must return an iterative string, and you can return a string because the string is iterative, but this causes the server to transfer your content by one character per character, at which point the Unicode character will not be allowed to be returned. That's certainly not going to Work.
Bottle supports more data types, It even adds a Content-Length
header message, and automatically encodes Unicode data, listing the types of data you can return in the Bottle application, and simply describes how the data of these data types are Bottle Processed by:
Data Type |
Introduction |
Dictionary (dictionaries) |
Python's built-in dictionary type data is automatically converted to a JSON string, and Content-Type the header information added as ' Application/json ' is returned to the browser, which allows us to easily build a json-based API |
An empty string, false,none, or any non-real data |
Bottle will create a contentlength header file for this type of data, set to 0 to return to the browser |
Unicode string |
The Unicode string is automatically encoded in the encoding format defined in the Content-type header file (default is UTF8), followed by normal string processing |
byte string (byte Strings) |
Bottle returns the entire string (rather than one return per byte), while increasing the Content-length header file to indicate the length of the byte string |
HTTPError and HTTPResponse Instance |
Returning these instances is like throwing an exception, and for httperror, the error will be handled with the correlation function |
File Object |
.read() the object with the method is then treated as a file or a File-like object and routed to the WSGI Server framework to define the wsgi.file_wrapper callback function, and some WSGI servers use the System-optimized request method (Sendfile) to send the File. |
Iterators and Raw Products |
You can use or return an iterator in your callback function, yield as long as the yield object is a string, a Unicode string, a httperror or a httpresponse object, but does not allow nested iterators, It is important to note that when The value of yield for the first time is non-null, and the HTTP status and header files will be sent to the browser |
If you return an str
instance of a class subclass with a read()
method, it will still be processed as a string because the string has a higher precedence.
Change the default encoding
Bottle encodes a string according to the parameters in the Content-type header file, which charset
defaults to text/html; charset=UTF8
, and can be Response.content_type
modified by attributes, or directly by Response.charset
property:
from bottle import response@route(‘/iso‘)def get_iso(): response.charset = ‘ISO-8859-15‘ return u‘This will be sent with ISO-8859-15 encoding.‘@route(‘/latin9‘)def get_latin(): response.content_type = ‘text/html; charset=latin9‘ return u‘ISO-8859-15 is also known as latin9.‘
For some rare reasons, the Python-encoded name may not be the same as the HTTP-encoded name, and you will need to do two things first to set up the Response.content_type
header file and then set it Response.charset
.
static files
You can return the file directly, but Bottle recommends using the static_file()
method, which automatically guesses the File's mime-type, appends the Last-Modified
header file, fully customizes the file path required for the service, and can handle errors (such as 404), and it also supports If-Modified-Since
Header file and can return 304 Not Modified
a response, you can also use a custom Mime-type to override the Mime-type guessing Value.
from bottle import static_file@route(‘/images/:filename#.*\.png#‘)def send_image(filename): return static_file(filename, root=‘/path/to/image/files‘, mimetype = ‘image/png‘)@route(‘/static/:filename‘)def send_static(filename): return static_file(filename, root=‘/path/to/static/files‘)
If you really need it, you can also throw the file in the form of an Exception.
Force download
Most browsers will automatically open the file if they know the MIME type of the downloaded file and the file type is bound to an application (such as a PDF file), and if you don't want to do so, you can force the browser to download it.
@route(‘/download/:filename‘)def download(filename): return static_file(filename, root=‘/path/to/static/files‘, download=filename)
Web development with bottle (5): generating Content