Presenter: James Huang Date: Sept. 29, 2014 1.  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.

Slides:



Advertisements
Similar presentations
1. XP 2 * The Web is a collection of files that reside on computers, called Web servers. * Web servers are connected to each other through the Internet.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
LIS651 lecture 3 taming PHP Thomas Krichel
LIS651 lecture 3 functions & sessions Thomas Krichel
7 Copyright © 2005, Oracle. All rights reserved. Maintaining State in J2EE Applications.
Powerpoint Templates Page 1 Powerpoint Templates Server Side Scripting PHP.
Hypertext Transfer PROTOCOL ----HTTP Sen Wang CSE5232 Network Programming.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
DB Relay An Introduction. INSPIRATION Database access is WAY TOO HARD The crux.
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
PHP syntax basics. Personal Home Page This is a Hypertext processor It works on the server side It demands a Web-server to be installed.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
HTTP HyperText Transfer Protocol. HTTP Uses TCP as its underlying transport protocol Uses port 80 Stateless protocol (i.e. HTTP Server maintains no information.
How the web works: HTTP and CGI explained
The World Wide Web and the Internet Dr Jim Briggs 1WUCM1.
1 K. Salah Module 2.1: Application Layer Application-level protocols provide high-level services –Web and HTTP –DNS –Electronic mail –Remote login –FTP.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
2/9/2004 Web and HTTP February 9, /9/2004 Assignments Due – Reading and Warmup Work on Message of the Day.
Hypertext Transport Protocol CS Dick Steflik.
 What is it ? What is it ?  URI,URN,URL URI,URN,URL  HTTP – methods HTTP – methods  HTTP Request Packets HTTP Request Packets  HTTP Request Headers.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
FTP (File Transfer Protocol) & Telnet
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CP476 Internet Computing Lecture 5 : HTTP, WWW and URL 1 Lecture 5. WWW, HTTP and URL Objective: to review the concepts of WWW to understand how HTTP works.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
Web application architecture
How Web Servers and the Internet Work by by: Marshall Brainby: Marshall Brain
XHTML Introductory1 Linking and Publishing Basic Web Pages Chapter 3.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
1 Welcome to CSC 301 Web Programming Charles Frank.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Prof Frankl, Spring 2008CS Polytechnic University 1 Overview of Web database applications with PHP.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
CITA 310 Section 2 HTTP (Selected Topics from Textbook Chapter 6)
Web Server.
Web Services. 2 Internet Collection of physically interconnected computers. Messages decomposed into packets. Packets transmitted from source to destination.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Chapter 22 World Wide Web (HTTP) Chapter 22 World Wide Web (HTTP) Mi-Jung Choi Dept. of Computer Science and Engineering
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
Tiny http client and server
HTTP – An overview.
Web Development Web Servers.
How does it work ?.
Play Framework: Introduction
Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Hypertext Transport Protocol
Client / Session Identification Cookies
Client / Session Identification Cookies
EE 122: HyperText Transfer Protocol (HTTP)
Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Presentation transcript:

Presenter: James Huang Date: Sept. 29,

 HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors and Redirects  Cookies  Templates  Plugins  Demo 2

 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

 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

 An HTTP session is a sequence of request- response transactions. 5 GET /index.html HTTP/1.1 Host: HTTP/ OK Date: Mon, 29 Sep :50:21 GMT Server: WSGIServer/0.1 Python/2.7.2 Content-Length: 32 Content-Type: text/html; charset=UTF-8 Hello World Web Server Web Client

 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 # A simple web application from bottle import run, 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

 URLs are linked to callback functions with route() decorator.  URL contains wildcards, dynamic route, can be used to match more than one ’) def hello(name): return greet(name)  More than one route can be used for a single ’) def hello(name=‘Stranger’): return greet(name) 8

 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

 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 ') def send_image(filename): return static_file(filename, root='/path/to/image/files', ') def server_static(filepath): return static_file(filepath, root='/path/to/your/html/files') match URL with regular expression match URL with path

 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. ◦ method=‘POST’)  Bottle also provides different decorators to handle different etc. as shortcuts.  The value of an Input field of a form can be retrieved with request object. 11

from bottle import get, post, request # or # def login(): return ''' Username: Password: # 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

 The abort() function is used to generate an HTTP error page. from bottle import route, def restricted(): abort(401, "Sorry, access denied.")  The redirect() function redirects a client to a different URL. from bottle import def wrong(): redirect("/right/url") 13

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

 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

 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? ') def hello(name): return template('hello_template', name=name) Python-like syntax hello_template.tpl

 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 ') 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.

Thank you! contact: 18