A Brief Introduce to WSGI

4,232 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,232
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
69
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

A Brief Introduce to WSGI

  1. 1. Simplicity: A Brief Introduce to WSGI Mingli Yuan Sept, 2010 Release under CC-By-SA
  2. 2. Contents <ul><li>Retrospect 1: Web server </li></ul><ul><li>Retrospect 2: APIs </li></ul><ul><li>What is WSGI? </li></ul><ul><li>WSGI Family </li></ul><ul><li>The WSGI Model </li></ul><ul><li>Middleware </li></ul><ul><li>Further Readings </li></ul><ul><li>References </li></ul>
  3. 3. Retrospect 1: Web server <ul><li>A  web server  is a  computer program  that delivers (serves) content, such as  web pages , using the  Hypertext Transfer Protocol  (HTTP), over the  World Wide Web . </li></ul><ul><li>Two programming models to implements a web server </li></ul><ul><ul><li>Thread model: Classic Java servlet container </li></ul></ul><ul><ul><li>Event model: Node.js </li></ul></ul><ul><li>A long history of the debates between the two models. [1][2] </li></ul>
  4. 4. Retrospect 2: APIs <ul><li>CGI: process based [3] </li></ul><ul><li>FastCGI: eliminate the processes burden [4] </li></ul><ul><li>Java Servlet API: OO style, application logic is encapsulated in a servlet class [5] </li></ul>
  5. 5. What is WSGI <ul><li>A question: What is a good abstraction for the logic between web server and application? </li></ul><ul><li>WSGI is a  specification  [6] for connecting HTTP servers with web applications and frameworks. </li></ul>WSGI App Framework Web Server
  6. 6. WSGI Family <ul><li>2003, The original python version [6] </li></ul><ul><li>2007, Rack, Ruby version [7] </li></ul><ul><li>2008, Lua WSAPI, Lua version [8] </li></ul><ul><li>2009, JSGI, Javascript version [9] </li></ul><ul><li>2009, PSGI, Perl version [10] </li></ul><ul><li>2010, Hack, Haskell version [11] </li></ul>
  7. 7. The WSGI Model <ul><li>Server side: </li></ul><ul><ul><li>We do not discuss this topic for now </li></ul></ul><ul><li>Application side: </li></ul><ul><ul><li>Application (a function) </li></ul></ul><ul><ul><ul><li>Environment -> (status, headers, body) </li></ul></ul></ul><ul><ul><li>Environment (a data structure) </li></ul></ul><ul><ul><ul><li>Hold request and other environment data </li></ul></ul></ul><ul><ul><li>Middleware (a function) </li></ul></ul><ul><ul><ul><li>Application -> Application </li></ul></ul></ul>
  8. 8. Examples 1 <ul><li>Hello World! [12] </li></ul><ul><li>def application(environ, start_response): </li></ul><ul><li>start_response(&quot;200 OK&quot;, [(&quot;Content-type&quot;, &quot;text/plain&quot;)]) </li></ul><ul><li>return [&quot;Hello World!&quot;,] </li></ul><ul><li>Sending a large file [12] </li></ul><ul><li>def application(environ, start_response): </li></ul><ul><li>size = os.path.getsize(file_path) </li></ul><ul><li>headers = [ </li></ul><ul><li>(&quot;Content-type&quot;, mimetype), </li></ul><ul><li>(&quot;Content-length&quot;, str(size)), </li></ul><ul><li>] </li></ul><ul><li>start_response(&quot;200 OK&quot;, headers) </li></ul><ul><li>return send_file(file_path, size) </li></ul><ul><li>def send_file(file_path, size): </li></ul><ul><li>with open(file_path) as f: </li></ul><ul><li>block = f.read(BLOCK_SIZE) </li></ul><ul><li>while block: </li></ul><ul><li>yield block </li></ul><ul><li>block = f.read(BLOCK_SIZE) </li></ul>
  9. 9. Middleware <ul><li>Url routes </li></ul><ul><ul><li>Rack::URLMap </li></ul></ul><ul><li>Logging </li></ul><ul><ul><li>Rack::CommonLogger </li></ul></ul><ul><li>Exception handling </li></ul><ul><ul><li>Rack::ShowException </li></ul></ul><ul><li>Static files </li></ul><ul><ul><li>Rack::File </li></ul></ul><ul><li>ETag </li></ul><ul><ul><li>Rack::ETag </li></ul></ul><ul><li>Auth </li></ul><ul><ul><li>Rack::Auth </li></ul></ul><ul><li>And others </li></ul><ul><ul><li>Rack::JSONP </li></ul></ul><ul><ul><li>Rack::LighttpdScriptNameFix </li></ul></ul><ul><ul><li>Rack::Locale </li></ul></ul><ul><ul><li>Rack::MailExceptions </li></ul></ul><ul><ul><li>Rack::NestedParams </li></ul></ul><ul><ul><li>Rack::PostBodyContentTypeParser </li></ul></ul><ul><ul><li>Rack::ProcTitle </li></ul></ul><ul><ul><li>Rack::Profiler </li></ul></ul><ul><ul><li>Rack::Sendfile </li></ul></ul><ul><ul><li>Rack::Signals </li></ul></ul><ul><ul><li>Rack::StaticCache </li></ul></ul><ul><ul><li>Rack::TimeZone </li></ul></ul><ul><ul><li>Rack::Evil </li></ul></ul><ul><ul><li>Rack::Callbacks </li></ul></ul><ul><ul><li>Rack::Config </li></ul></ul><ul><ul><li>Rack::NotFound </li></ul></ul><ul><ul><li>Rack::CSSHTTPRequest </li></ul></ul><ul><ul><li>Rack::Deflect </li></ul></ul><ul><ul><li>Rack::ResponseCache </li></ul></ul><ul><ul><li>Rack::RelativeRedirect </li></ul></ul><ul><ul><li>Rack::Backstage </li></ul></ul><ul><ul><li>Rack::AcceptFormat </li></ul></ul><ul><ul><li>Rack::HostMeta </li></ul></ul><ul><ul><li>Rack::Cookies </li></ul></ul><ul><ul><li>Rack::Access </li></ul></ul><ul><ul><li>Rack::ResponseHeaders </li></ul></ul><ul><ul><li>Rack::SimpleEndpoint </li></ul></ul>
  10. 10. Examples 2 <ul><li>module Rack   class Upcase     def initialize app       @app = app     end          def call env       puts 'upcase'       p @app       puts       status, headers, body = @app.call env       [status, headers, [body.first.upcase]]     end   end </li></ul><ul><li>end </li></ul><ul><li>module Rack   class Reverse     def initialize app       @app = app     end          def call env       puts 'reverse'       p @app       puts       status, headers, body = @app.call env       [status, headers, [body.first.reverse]]     end   end </li></ul><ul><li>end </li></ul><ul><li>use Rack::Upcase </li></ul><ul><li>use Rack::Reverse </li></ul><ul><li>use Rack::ContentLength </li></ul><ul><li>app = lambda { |env| [200, { 'Content-Type' => 'text/html' }, 'Hello World'] } </li></ul><ul><li>run app </li></ul>
  11. 11. Further Readings <ul><li>WSGI Tutorial </li></ul><ul><li>Benchmark of Python WSGI Servers </li></ul><ul><li>uWSGI </li></ul>
  12. 12. Some Thoughts <ul><li>Simplicity </li></ul><ul><li>OO is not silver bullet </li></ul><ul><ul><li>function as first class member in a programming language </li></ul></ul><ul><ul><li>sometimes OO style is too burden </li></ul></ul><ul><li>Open culture </li></ul><ul><ul><li>The Cathedral and the Bazaar </li></ul></ul>
  13. 13. References <ul><li>[1] Why Thread Are A Bad Idea, John Ousterhout , 1996 USENIX Technical Conference, 1996 </li></ul><ul><li>[2] Why Event Are A Bad Idea, Rob von Behren, et al. , Proceedings of HotOS IX, 2003 </li></ul><ul><li>[3] RFC3875: The Common Gateway Interface (CGI) Version 1.1 </li></ul><ul><li>[4] FastCGI specification </li></ul><ul><li>[5] Java™ Servlet API </li></ul><ul><li>[6] PEP 333 Python Web Server Gateway Interface v1.0 </li></ul><ul><li>[7] Rack Specification </li></ul><ul><li>[8] WSAPI Specification </li></ul><ul><li>[9] JSGI Specification </li></ul><ul><li>[10] PSGI Specification </li></ul><ul><li>[11] Hack Packages </li></ul><ul><li>[12] WSGI Tutorial </li></ul><ul><li>[13] Ruby Rack Middleware Tutorial </li></ul>

×