“I drink my WSGI clear” A barebones introduction to Python Web Programming. 3/14/2011.

Slides:



Advertisements
Similar presentations
XPointer and HTTP Range A possible design for a scalable and extensible RDF Data Access protocol. Bryan Thompson draft Presented to the RDF.
Advertisements

1 CGICGI Common Gateway Interface Server-side Programming Lecture.
COEN 445 Communication Networks and Protocols Lab 4
Common Gateway Interface (CGI). CGI is a protocol: CGI is not a programming language CGI is a protocol for the exchange of information between between.
HTTP HyperText Transfer Protocol. HTTP Uses TCP as its underlying transport protocol Uses port 80 Stateless protocol (i.e. HTTP Server maintains no information.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Web Servers How do our requests for resources on the Internet get handled? Can they be located anywhere? Global?
How the web works: HTTP and CGI explained
SOAP Chandra Dutt Yarlagadda Introduction  Why ?  What ?  How ?  Security Issues in SOAP  Advantages  Uses  Conclusion.
Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley, July.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
2/9/2004 Web and HTTP February 9, /9/2004 Assignments Due – Reading and Warmup Work on Message of the Day.
 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.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
Web Client/Server Communication A290/A590, Fall /09/2014.
Platform as a Service (PaaS)
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
J2EE Web Fundamentals Lesson 1 Introduction and Overview
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Google App Engine Chien-Chung Shen
FTP (File Transfer Protocol) & Telnet
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
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.
2: Application Layer1 CS 4244: Internet Software Development Dr. Eli Tilevich.
Cloud computing lectures: Programming with Google App Engine Keke Chen.
A Closer Look at HTTP HyperText Transfer Protocol.
Copyright (c) 2010, Dr. Kuanchin Chen1 The Client-Server Architecture of the WWW Dr. Kuanchin Chen.
Putting What We Learned Into Context – WSGI and Web Frameworks A290/A590, Fall /16/2014.
from SimpleHTTPServer import SimpleHTTPRequestHandler import SocketServer httpd = SocketServer.TCPServer((‘’, 8000), SimpleHTTPRequestHandler) httpd.serve_forever()
James Holladay, Mario Sweeney, Vu Tran. Web Services Presentation Web Services Theory James Holladay Tools – Visual Studio Vu Tran Tools – Net Beans Mario.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
ELECTRONIC COMMERCE- Framework, Technologies and Applications © Tata McGraw-Hill 1 Electronic Commerce: Information Distribution and Messaging.
First Indico Workshop WEB FRAMEWORKS Adrian Mönnich May 2013 CERN.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
1 Web Services Web and Database Management System.
1-1 HTTP request message GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr request.
LinuxChix Apache. Serving Webpages The layer 7 protocol (HTTP) is what our browsers talk to get us the websites we can't seem to live without. HTTP is.
Form Data Encoding GET – URL encoded POST – URL encoded
COWS CEDA OGC Web Services Framework Stephen Pascoe.
Google Application Engine Introduction Jim Eng with thanks to Charles Severance
Web Server Design Assignment #2: Conditionals & Persistence Due: 02/24/2010 Old Dominion University Department of Computer Science CS 495/595 Spring 2010.
Appendix E: Overview of HTTP ©SoftMoore ConsultingSlide 1.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
Martin Kruliš by Martin Kruliš (v1.1)1.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications  app architectures  app requirements r 2.2 Web and HTTP.
CITA 310 Section 2 HTTP (Selected Topics from Textbook Chapter 6)
Philip Repsher October 29 th, 2008 Or Maybe November 3 rd, 2008.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 7 Omar Meqdadi Department of Computer Science and Software Engineering University of.
Week 11: Application Layer 1 Web and HTTP r Web page consists of objects r Object can be HTML file, JPEG image, Java applet, audio file,… r Web page consists.
Server-side http General form of http response/request GET request method POST request method Responses Servlet support.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
SOAP, Web Service, WSDL Week 14 Web site:
Python: Programming the Google Search (Crawling) Damian Gordon.
APIs George Wyner January 28, Agenda What is an API? How do developers discover, understand, and use APIs to build applications How to use an API.
J2EE vs Python for web development David
Platform as a Service (PaaS)
Platform as a Service (PaaS)
Platform as a Service (PaaS)
Web Development Web Servers.
Upnl 24th workshop kim jae chan
Development and Deployment with WSGI in Django
Web Server Design Assignment #5 Extra Credit
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS/COE 1520 Jarrett Billingsley
Presentation transcript:

“I drink my WSGI clear” A barebones introduction to Python Web Programming. 3/14/2011

Who are we? Doug Morgan Alex Conrad Whit Morriss

What is WSGI?

WSGI Web Server Gateway Interface, pronounced ‘whizky’ (duh) PEP 333 A standardized specification to define communication between a web server and your Python application.

What do you need? An HTTP server with WSGI support aka a Host (we’ll get into these later). A Python file (your app).

How does it work? 1) An HTTP request comes in to your server. 2) The HTTP server calls your Python app with two arguments: The environment and the “start response” function. 3) Your application returns a Response body which is sent back to the client. 4) … 5) Profit!

Example def application(environ, start_response): # Let’s setup some data to send back to the client body = “WSGI is awesome and tastes delicious!” headers = [(“Content-Type”, “text/plain”), (“Content-Length, str(len(body)))] # Send response headers back to the client start_response(“200 OK”, headers) # Send the body back to the client to complete the Response return body

Example def application(environ, start_response): # Let’s setup some data to send back to the client body = “WSGI is awesome and tastes delicious!” headers = [(“Content-Type”, “text/plain”), (“Content-Length, str(len(body)))] # Send response headers back to the client start_response(“200 OK”, headers) # Send the body back to the client to complete the Response return body

Example def application(environ, start_response): # Let’s setup some data to send back to the client body = “WSGI is awesome and tastes delicious!” headers = [(“Content-Type”, “text/plain”), (“Content-Length, str(len(body)))] # Send response headers back to the client start_response(“200 OK”, headers) # Send the body back to the client to complete the Response return body

Example def application(environ, start_response): # Let’s setup some data to send back to the client body = “WSGI is awesome and tastes delicious!” headers = [(“Content-Type”, “text/plain”), (“Content-Length, str(len(body)))] # Send response headers back to the client start_response(“200 OK”, headers) # Send the body back to the client to complete the Response return body

Arguments & Return Value environ: A dictionary (or Map if you’re from the Java land) of data about the incoming request, i.e. HTTP Headers, HTTP Host, etc. {"HTTP_HOST": "surveymonkey.com", “HTTP_PATH”: “/surveys” "REQUEST_METHOD": "GET", …} start_response: A Python function supplied by the server that your app calls to send headers back to the client. start_response("200 OK", headers) What your app returns will be the response body. return " WSGI rules "

WSGI Hosts Python comes with a built-in one that you can use, wsgiref, batteries included. Other WSGI Hosts: Apache mod_wsgi Google App Engine Paste (Python) uWsgi (Nginx, Cherokee, Apache)

WSGIRef Example docs.python.org/library/wsgiref.html run the following as “python server.py” from wsgiref.simple_server import make_server def application(environ, start_response): …. (from previous example) # Server application on port 8000 httpd = make_server(‘’, 8000, application) httpd.serve_forever() # run into process is killed

That’s it!

WSGI on the rocks

WSGI on the Rocks Libraries providing basic abstraction over raw WSGI providing request and response classes: Webob (apt-get install python-webob or easy_install webob) Werkzeug

WebOb Example from webob import Response from webob.dec import def hello_world_app(request): response = Response(“Hello World”) return response

WebOb Example from webob import Response from webob.dec import def hello_world_app(request): response = Response(“Hello World”) return response

And now for something more useful

WebOb URL def main_app(request): path = request.environ[‘PATH_INFO’] if path in app_paths: return app_paths[path](request) return response.status = 404 def foo_app(request): return Response(“Got into /foo”) app_paths = {‘/foo’: foo_app, ‘/bar’: bar_app, …}

WebOb URL def main_app(request): path = request.environ[‘PATH_INFO’] if path in app_paths: return app_paths[path](request) return response.status = 404 def foo_app(request): return Response(“Got into /foo”) app_paths = {‘/foo’: foo_app, ‘/bar’: bar_app, …}

WebOb URL def main_app(request): path = request.environ[‘PATH_INFO’] if path in app_paths: return app_paths[path](request) return response.status = 404 def foo_app(request): return Response(“Got into /foo”) app_paths = {‘/foo’: foo_app, ‘/bar’: bar_app, …}

WebOb URL def main_app(request): path = request.environ[‘PATH_INFO’] # ‘/foo’ if path in app_paths: return app_paths[path](request) return response.status = 404 def foo_app(request): return Response(“Got into /foo”) app_paths = {‘/foo’: foo_app, ‘/bar’: bar_app, …}

WebOb URL def main_app(request): path = request.environ[‘PATH_INFO’] if path in app_paths: return app_paths[path](request) return response.status = 404 def foo_app(request): return Response(“Got into /foo”) app_paths = {‘/foo’: foo_app, ‘/bar’: bar_app, …}

WebOb URL def main_app(request): path = request.environ[‘PATH_INFO’] if path in app_paths: return app_paths[path](request) return response.status = 404 def foo_app(request): return Response(“Got into /foo”) app_paths = {‘/foo’: foo_app, ‘/bar’: bar_app, …}

Middleware What we just built, a routing interface, is a ‘middleware’. A middleware is a WSGI application that sits between the web server the your main application which can do pre and post processing on the request and response. Takes as input, the request object, and the next application to call.

Uppercase Middleware Example def app(req): response = Response("The quick brown fox jumped over buzz.") return def upper_middleware(request, app): response = app(request) response.body = response.body.upper() return response wrapper_app = upper_middleware(app) httpd = make_server("", 8000, wrapper_app)

Uppercase Middleware Example def app(req): response = Response("The quick brown fox jumped over buzz.") return def upper_middleware(request, app): response = app(request) response.body = response.body.upper() return response wrapper_app = upper_middleware(app) httpd = make_server("", 8000, wrapper_app)

Uppercase Middleware Example def app(req): response = Response("The quick brown fox jumped over buzz.") return def upper_middleware(request, app): response = app(request) response.body = response.body.upper() return response wrapper_app = upper_middleware(app) httpd = make_server("", 8000, wrapper_app)

Gzip Middleware Example def gzip_middleware(request, app): # Don’t want to do any pre-processing so just call the app response = app(request) # Compress the body and set header response.body = gzip(request.body) response.headers[‘Content-Encoding’] = ‘gzip’ return response

Middleware Considerations Two things: Middlewares are not aware of anything before or after them in the ‘pipeline.’ Middlewares should be reusable. The middleware you write should act more like a filter as opposed to having a lot of business logic. Rule of thumb: if the code is specific to your application (not reusable), it should probably be a library and not a middleware.

That was so easy... A cavema... Err anyone can do it! There are dozens of web frameworks that are available (most are open-source) for your use which do a lot of the leg work for you. Examples: Pylons/Pyramid (What we use at SurveyMonkey) Django TurboGears Repoze.bfg Bottle Flask

Questions?