Advertisement
Guest User

program 2

a guest
Nov 27th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. Building Web Applications -WSGI
  2.  
  3. WSGI ("wheez-gee") is a Python specification that describes how web server communicates with web applications, and how web applications can be chained together to process one request.
  4.  
  5. Hello World WSGI Application, access it via http://localhost:8000/
  6.  
  7.  
  8.  
  9. from wsgiref.simple_server import make_server
  10.  
  11. def hello_world_app(environ, start_response):
  12. status = '200 OK'
  13. headers = [('Content-type', 'text/plain; charset=utf-8')]
  14. start_response(status, headers)
  15.  
  16. return[b"Hello World"]
  17.  
  18. httpd = make_server('', 8000, hello_world_app)
  19. print("Serving on port 8000...")
  20.  
  21.  
  22. httpd.serve_forever()
  23.  
  24.  
  25. environ parameter to your handler function is the dictionary of variables from the server, start_response variable is the interface for sending HTTP status and response headers.
  26.  
  27. Let's modify the server to serve HTML content:
  28.  
  29.  
  30.  
  31. from wsgiref.simple_server import make_server
  32.  
  33. def hello_world_app(environ, start_response):
  34. status = '200 OK'
  35. headers = [('Content-type', 'html; charset=utf-8')]
  36. start_response(status, headers)
  37. message = "<h1>Welcome to the Zoo</h1>"
  38. return[bytes(message,'utf-8')]
  39.  
  40. httpd = make_server('', 8000, hello_world_app)
  41. print("Serving on port 8000...")
  42.  
  43.  
  44. httpd.serve_forever()
  45.  
  46.  
  47. Building the form to accept user input:
  48.  
  49.  
  50.  
  51. Welcome to the Zoo
  52.  
  53.  
  54. Animal:
  55.  
  56. Count:
  57.  
  58.  
  59.  
  60. Please view the code
  61. 1 from wsgiref.simple_server import make_server
  62. 2
  63.  
  64. 3 def hello_world_app(environ, start_response):
  65. 4 message=""
  66. 5 status = '200 OK'
  67. 6 headers = [('Content-type', 'html; charset=utf-8')]
  68. 7 start_response(status, headers)
  69. 8 message += "<h1>Welcome to the Zoo</h1>"
  70. 9 message += "<form><br>Animal:<input type=text name='animal'>"
  71. 10 message += "<br><br>Count:<input type=text name='count'>"
  72. 11 message += "<br><br><input type='submit' name='Submit' ></form>"
  73. 12 return[bytes(message,'utf-8')]
  74. 13
  75.  
  76. 14 httpd = make_server('', 8000, hello_world_app)
  77. 15 print("Serving on port 8000...")
  78. 16
  79.  
  80. 17
  81.  
  82. 18 httpd.serve_forever()
  83.  
  84. As of right now, the form doesn't do anything.
  85.  
  86. Submitting the form directs the URL to itself and passes the values entered in the form as GET parameters in the URL:
  87.  
  88. http://localhost:8000/?animal=Hedgehog&count=4&Submit=Submit
  89.  
  90.  
  91. Let's take a look at the environ variable that gets passed to our hello_world_app, I added the print("ENVIRON:", environ) statement to see what we get as part of it (all not related data is removed)
  92.  
  93. 127.0.0.1 - - [02/Nov/2014 09:15:00] "GET / HTTP/1.1" 200 170
  94.  
  95. ENVIRON: {'HTTP_REFERER': 'http://localhost:8000/', 'SERVER_SOFTWARE': 'WSGIServer/0.2', 'HTTP_HOST': 'localhost:8000', 'QUERY_STRING': 'animal=Hedgehog&count=4&Submit=Submit', 'REQUEST_METHOD': 'GET', ...}
  96.  
  97. add method='POST' in your form tag, and view the environ variable again:
  98.  
  99. Now we have:
  100. 'REQUEST_METHOD': 'POST'
  101.  
  102.  
  103. and the QUERY_STRING is gone, which is ok, it's applicable to GET method only, our data now resides in the
  104. 'wsgi.input' object
  105.  
  106.  
  107. Let's modify our application such that it displays the data it has received back with the confirmation of submission,
  108.  
  109. code is here:
  110. 1 from wsgiref.simple_server import make_server
  111. 2
  112.  
  113. 3 def get_form_vals(post_str):
  114. 4 form_vals = {item.split("=")[0]: item.split("=")[1] for item in post_str.decode().split("&")}
  115. 5 return form_vals
  116. 6
  117.  
  118. 7 def hello_world_app(environ, start_response):
  119. 8 #print("ENVIRON:", environ)
  120. 9 message=""
  121. 10 status = '200 OK'
  122. 11 headers = [('Content-type', 'html; charset=utf-8')]
  123. 12 start_response(status, headers)
  124. 13 if(environ['REQUEST_METHOD'] == 'POST'):
  125. 14 message += "<br>Your data has been recorded:"
  126. 15 request_body_size = int(environ['CONTENT_LENGTH'])
  127. 16 request_body = environ['wsgi.input'].read(request_body_size)
  128. 17 form_vals = get_form_vals(request_body)
  129. 18 for item in form_vals.keys():
  130. 19 message += "<br/>"+item + " = " + form_vals[item]
  131. 20 message += "<h1>Welcome to the Zoo</h1>"
  132. 21 message += "<form method='POST'><br>Animal:<input type=text name='animal'>"
  133. 22 message += "<br><br>Count:<input type=text name='count'>"
  134. 23 message += "<br><br><input type='submit' name='Submit' ></form>"
  135. 24 return[bytes(message,'utf-8')]
  136. 25
  137.  
  138. 26 httpd = make_server('', 8000, hello_world_app)
  139. 27 print("Serving on port 8000...")
  140. 28
  141.  
  142. 29
  143.  
  144. 30 httpd.serve_forever()
  145.  
  146.  
  147.  
  148. Your data has been recorded:
  149. animal = Hedgehog
  150. count = 4
  151. Submit = Submit
  152. Welcome to the Zoo
  153.  
  154.  
  155. Animal:
  156.  
  157. Count:
  158.  
  159.  
  160.  
  161. Processing the GET parameters is simpler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement