Download presentation
Presentation is loading. Please wait.
Published byOsvaldo Ralphs Modified over 10 years ago
1
Presenter: James Huang Date: Sept. 29, 2014 1
2
HTTP and WWW Bottle Web Framework Request Routing Sending Static Files Handling HTML HTTP Errors and Redirects Cookies Templates Plugins Demo 2
3
World Wide Web (WWW) uses Hypertext Transfer Protocol (HTTP) as a standard for web client and web server to communicate. HTTP uses a request-response computing model. ◦ A server listens for clients’ connection over TCP. ◦ A client (e.g. web browser) sends a request message to server. ◦ The server sends back a status line followed by a response message. HTTP is a stateless protocol. ◦ Web applications often use cookies to maintain states. 3
4
An HTTP client specifies request methods to distinguish different types of actions to be performed on the server. GET ◦ Retrieves data from the server ◦ Should not have other effect on the server POST ◦ Requests that the server accepts the data enclosed in the request ◦ Used to submit a form, upload a file, etc. 4
5
An HTTP session is a sequence of request- response transactions. 5 GET /index.html HTTP/1.1 Host: www.example.com HTTP/1.0 200 OK Date: Mon, 29 Sep 2014 03:50:21 GMT Server: WSGIServer/0.1 Python/2.7.2 Content-Length: 32 Content-Type: text/html; charset=UTF-8 Hello World. 1. 2. Web Server Web Client
6
Bottle is a web server that allows developers to write web applications in Python. It is easy to install and use. ◦ Implemented in Python ◦ Has no dependency on any external libraries Writing web application for Bottle is simple. ◦ A web application consists of callback functions that are called by Bottle to handle HTTP requests. ◦ Bottle handles errors and exceptions automatically. 6
7
7 # A simple web application from bottle import run, route @route('/') def home(): return ' Hello World. ‘ run(host='localhost', port=8080, debug=True) hello.py: Links ‘/’ to home() Starts web server on port 8080 Imports Bottle web framework
8
URLs are linked to callback functions with route() decorator. URL contains wildcards, dynamic route, can be used to match more than one URL. @route(‘/hello/ ’) def hello(name): return greet(name) More than one route can be used for a single callback function. @route(‘/’) @route(‘/hello/ ’) def hello(name=‘Stranger’): return greet(name) 8
9
A simple wildcard (e.g. ) matches one or more characters up to the next slash (‘/’). ◦ E.g. ‘/hello/ ’ matches ‘/hello/bob’, but not ‘/hello’ or ‘/hello/john/smith’. Each wildcard passes the matched part of URL as a keyword argument to the callback function. Filters can be used to match specific wildcards and converts matched part of URL. ◦ :int – matches digits only and converts the value to integer ◦ :path – matches more than one path segment ◦ :re – matches with a regular expression 9
10
A web application needs to have a callback to handle which files to serve to a client. ◦ Bottle does not automatically serve files to client. Bottle provides static_file() helper function. 10 from bottle import static_file @route('/images/ ') def send_image(filename): return static_file(filename, root='/path/to/image/files', mimetype='image/png') @route('/html/ ') def server_static(filepath): return static_file(filepath, root='/path/to/your/html/files') match URL with regular expression match URL with path
11
HTML usually specify to use POST request method to send form data. Bottle’s route decorator applies to GET request method by default. A web application can specify request method as an argument of route decorator. ◦ E.g. @route(‘/login’, method=‘POST’) Bottle also provides different decorators to handle different request methods: @get, @post, etc. as shortcuts. The value of an Input field of a form can be retrieved with request object. 11
12
from bottle import get, post, request # or route @get('/login') # or @route('/login') def login(): return ''' Username: Password: ''' @post('/login') # or @route('/login', method='POST') def do_login(): username = request.forms.get('username') password = request.forms.get('password') if check_login(username, password): return " Your login information was correct. " else: return " Login failed. " 12 login() is called when user browse to /login do_login() is called when user submit the form request object is used to retrieve the value of an input field
13
The abort() function is used to generate an HTTP error page. from bottle import route, abort @route('/restricted') def restricted(): abort(401, "Sorry, access denied.") The redirect() function redirects a client to a different URL. from bottle import redirect @route('/wrong/url') def wrong(): redirect("/right/url") 13
14
A cookie stores site-specific text information in a web browser’s profile. A web application creates a new cookie with Response.set_cookie(), access a cookie with Request.get_cookie(). 14 @route('/hello') def hello_again(): if request.get_cookie("visited"): return "Welcome back! Nice to see you again" else: response.set_cookie("visited", "yes") return "Hello there! Nice to meet you"
15
A cookie can be controlled with different settings. ◦ max_age: maximum age in seconds ◦ expires: the timestamp when the cookie expires ◦ secure: limit the cookie to HTTPS connections These settings can be specified as keyword arguments to Response.set_cookie(). Cookie expires at the end of a browser session, unless max_age or expires is set. 15
16
Bottle provide a convenient way to generate web page from templates. Here is an example template: A web application uses templates with template() function. 16 %if name == 'World': Hello {{name}}! This is a test. %else: Hello {{name.title()}}! How are you? %end @route('/hello/ ') def hello(name): return template('hello_template', name=name) Python-like syntax hello_template.tpl
17
Bottle supports third-party plugins: ◦ Bottle-Sqlite: SQLite3 database plugin ◦ Bottle-Redis: Redis database plugin ◦ Bottle-Flash: flash plugin 17 from bottle import route, install, template from bottle_sqlite import SQLitePlugin install(SQLitePlugin(dbfile='/tmp/test.db')) @route('/show/ ') def show(db, post_id): c = db.execute('SELECT title, content FROM posts WHERE id = ?', (post_id,)) row = c.fetchone() return template('show_post', title=row['title'], text=row['content']) SQLitePlugin detects the use of db and creates a new database connection.
18
Thank you! contact: z.huang@utoronto.ca 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.