Introduction to HTTP and Web Servers

Introduction to HTTP and Web Servers
Slide Note
Embed
Share

The presentation covers the basics of HTTP, web servers, and web frameworks like Bottle. It explains how HTTP works, the client-server communication model, request methods, and sessions. Additionally, it delves into Bottle, a Python web server that simplifies web application development, highlighting its features and ease of use.

  • HTTP
  • Web Servers
  • Bottle
  • Python
  • Web Framework

Uploaded on Mar 06, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Presenter: James Huang Date: Sept. 29, 2014 1

  2. HTTP and WWW Bottle Web Framework Request Routing Sending Static Files Handling HTML <Form> 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. Web Server Web Client 1. GET /index.html HTTP/1.1 Host: www.example.com GET /index.html HTTP/1.1 Host: www.example.com 2. HTTP/1.0 Date: Mon, 29 Sep 2014 03:50:21 GMT Server: Content Content HTTP/1.0 200 OK Date: Mon, 29 Sep 2014 03:50:21 GMT Server: WSGIServer Content- -Length: Content- -Type 200 OK WSGIServer/0.1 Python/2.7.2 Length: 32 Type: text/html; /0.1 Python/2.7.2 32 : text/html; charset=UTF charset=UTF- -8 8 <html> <body> Hello World. </body> </html> <html> <body> Hello World. </body> </html> 5

  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. hello.py: hello.py: Imports Bottle web framework Imports Bottle web framework # # A A simple web simple web application application from @ @route def from bottle import route('/') def home return bottle import run, route ('/') home(): return '<html><body>Hello World.</body></html run, route Links / to home() Links / to home() (): '<html><body>Hello World.</body></html> > run(host='localhost', port=8080, debug=True) run(host='localhost', port=8080, debug=True) Starts web server on port 8080 Starts web server on port 8080 7

  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/<name> ) def hello(name): return greet(name) More than one route can be used for a single callback function. @route( / ) @route( /hello/<name> ) def hello(name= Stranger ): return greet(name) 8

  9. A simple wildcard (e.g. <name>) matches one or more characters up to the next slash ( / ). E.g. /hello/<name> 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. match URL with regular expression from bottle import static_file @route('/images/<filename:re:.*\.png>') def send_image(filename): return static_file(filename, root='/path/to/image/files', mimetype='image/png') @route('/html/<filepath:path>') def server_static(filepath): return static_file(filepath, root='/path/to/your/html/files') match URL with path 10

  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 name="username" type="text" /> Password: <input name="password" type="password" /> <input value="Login" type="submit" /> </form> ''' @ @post('/login') # or @route('/login', method='POST') def from bottle import get, post, request # or route get('/login') # or @route('/login') ') def login(): return name="username" type="text" /> Password: <input name="password" type="password" /> <input value="Login" type="submit" /> </form> ''' post('/login') # or @route('/login', method='POST') def do_login username password if return else return login() is called when user browse to /login login() is called when user browse to /login login(): return ''' <form action="/login" method="post"> Username: <input ''' <form action="/login" method="post"> Username: <input do_login(): username = password = if check_login return "<p>Your login information was correct.</p>" else: : return "<p>Login failed.</p>" (): = request.forms.get = request.forms.get check_login(username, password): "<p>Your login information was correct.</p>" do_login submit the form do_login() is called when user submit the form () is called when user request.forms.get('username') request.forms.get('password') (username, password): ('username') ('password') request object is used to retrieve the value of an input field request object is used to retrieve the value of an input field "<p>Login failed.</p>" 12

  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(). @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" 14

  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: %if name == 'World': <h1>Hello {{name}}!</h1> %else: %end Python-like syntax <p>This is a test.</p> <h1>Hello {{name.title()}}!</h1> <p>How are you?</p> A web application uses templates with template() function. @route('/hello/<name>') def hello(name): name=name) hello_template.tpl return template('hello_template', 16

  17. Bottle supports third-party plugins: Bottle-Sqlite: SQLite3 database plugin Bottle-Redis: Redis database plugin Bottle-Flash: flash plugin from bottle import route, install, template from install( @route('/show/< def ( (post_id from bottle import route, install, template from bottle_sqlite install(SQLitePlugin @route('/show/<post_id:int def show( c = post_id,)) row = return template(' SQLitePlugin detects the use of db and creates a new database connection. import SQLitePlugin dbfile='/ post_id:int>') db, , post_id c = db.execute ,)) row = c.fetchone return template('show_post bottle_sqlite import SQLitePlugin( (dbfile SQLitePlugin ='/tmp >') tmp/ /test.db test.db')) ')) show(db db.execute('SELECT title, content FROM posts WHERE id = ?', post_id): ('SELECT title, content FROM posts WHERE id = ?', ): c.fetchone() () show_post', title=row['title'], text=row['content']) ', title=row['title'], text=row['content']) 17

  18. Thank you! Thank you! contact: z.huang@utoronto.ca 18

More Related Content