分享

Openstack学习之WSGI:自己动手写例子

xioaxu790 发表于 2014-9-17 18:02:53 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 10989
问题导读
1、什么是WSGI ?
2、如何写一个WSGI 的hello world APP?
3、如何测试程序?




WSGI是Web Services Gateway Interface的缩写. 如果想深入了解,可以阅读 PEP 333 文档,包含有任何你想要的:)community errata, .这篇文章将手把手教你写一个简单的WSGI例子。


注意:我用的Python版本是.2.7.x.

最经典,最简答的WSGI样当属Hello World app

咱们将要用到 virtualenv 去创建一个封闭的Python项目环境:
  1. $
  2. virtualenv wsgi-example
  3. $
  4. cd wsgi-example
  5. $
  6. source bin/activate
复制代码


然后在该目录下新建一个文件: wsgi_app.py 并且添加如下的代码 :
  1. from
  2. __future__ import
  3. print_function
  4. from
  5. wsgiref.simple_server import
  6. make_server
  7. def
  8. myapp(environ, start_response):
  9.     response_headers
  10. =
  11. [('content-type',
  12. 'text/plain')]
  13.     start_response('200
  14. OK',
  15. response_headers)
  16.     return
  17. ['Hello
  18. World']
  19. app
  20. =
  21. myapp
  22. httpd
  23. =
  24. make_server('', 8080,
  25. app)
  26. print("Starting
  27. the server on port 8080")
  28. httpd.serve_forever()
复制代码



把项目跑起来:
  1. $
  2. python wsgi_app.py
复制代码


之后就可以测试啦,测试有浏览器或者命令cURL两种方式,返回的应该是hello world 哦:
  1. $
  2. curl http://localhost:8080 -v
复制代码


上面这个例子虽然清晰,但是一点都不爽啊so easy。let's high 起来。接下来我们继续往里面赛一条消息 。

修改 wsgi_app.py 文件成下面这个样纸:
  1. from
  2. __future__ import
  3. print_function
  4. from
  5. wsgiref.simple_server import
  6. make_server
  7. from
  8. urlparse import
  9. parse_qs
  10. def
  11. myapp(environ, start_response):
  12.     msg
  13. =
  14. 'No Message!'
  15.     response_headers
  16. =
  17. [('content-type',
  18. 'text/plain')]
  19.     start_response('200
  20. OK',
  21. response_headers)
  22.     qs_params
  23. =
  24. parse_qs(environ.get('QUERY_STRING'))
  25.     if
  26. 'msg'
  27. in
  28. qs_params:
  29.             msg
  30. =
  31. qs_params.get('msg')[0]
  32.     return
  33. ['Your
  34. message was: {}'.format(msg)]
  35. app
  36. =
  37. myapp
  38. httpd
  39. =
  40. make_server('', 8080,
  41. app)
  42. print("Starting
  43. the server on port 8080")
  44. httpd.serve_forever()
复制代码


把程序跑起来,测试还是刚才那样。如果程序还在运行中,可以CTRL+C快捷键暂停并且重启。
  1. $
  2. curl http://localhost:8080/?msg=Hello -v
复制代码


接下来,让我们继续聊中间件。你可以在这里 获得一些信息。这里我会写一个添加了HTTP 头回复的中间层例子。中间件可以做任何你能够想象的到的事情,例如session,认证等等。

中间件例子:
  1. from
  2. __future__ import
  3. print_function
  4. from
  5. wsgiref.simple_server import
  6. make_server
  7. def
  8. myapp(environ, start_response):
  9.     response_headers
  10. =
  11. [('content-type',
  12. 'text/plain')]
  13.     start_response('200
  14. OK',
  15. response_headers)
  16.     return
  17. ['Check
  18. the headers!']
  19. class
  20. Middleware:
  21.     def
  22. __init__(self,
  23. app):
  24.         self.wrapped_app
  25. =
  26. app
  27.     def
  28. __call__(self,
  29. environ, start_response):
  30.         def
  31. custom_start_response(status, headers, exc_info=None):
  32.             headers.append(('X-A-SIMPLE-TOKEN',
  33. "1234567890"))
  34.             return
  35. start_response(status, headers, exc_info)
  36.         return
  37. self.wrapped_app(environ,
  38. custom_start_response)
  39. app
  40. =
  41. Middleware(myapp)
  42. httpd
  43. =
  44. make_server('', 8080,
  45. app)
  46. print("Starting
  47. the server on port 8080")
  48. httpd.serve_forever()
复制代码


访问终端:
  1. $
  2. curl http://localhost:8080/ -v
复制代码


结果差不多应该是介个样子的:
  1. *
  2. Connected to localhost (127.0.0.1) port 8080 (#0)
  3. >
  4. GET / HTTP/1.1
  5. >
  6. User-Agent: curl/7.30.0
  7. >
  8. Host: localhost:8080
  9. >
  10. Accept: */*
  11. >
  12. *
  13. HTTP 1.0, assume close after body
  14. <
  15. HTTP/1.0 200 OK
  16. <
  17. Date: Sat, 08 Feb 2014 00:16:00 GMT
  18. <
  19. Server: WSGIServer/0.1 Python/2.7.5
  20. <
  21. content-type: text/plain
  22. <
  23. X-A-SIMPLE-TOKEN: 1234567890
  24. <
  25. Content-Length: 18
  26. <
  27. *
  28. Closing connection 0
复制代码




已有(2)人评论

跳转到指定楼层
anyhuayong 发表于 2014-9-18 09:14:37
学习下,谢楼主分享
回复

使用道具 举报

dsq58629 发表于 2014-9-18 09:45:05

支持啊~支持啊~支持啊~支持啊~

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条