Webob Request Response

本文地址:http://blog.csdn.net/spch2008/article/details/9001838


Request

Webob的Request对象,提供对WSGI environ环境变量的包装,通过webob可以很容易的读写environ字典。

environ字典内容如下:

TMP = C:\Users\spch2008\AppData\Local\Temp 
PYTHONIOENCODING = GBK 
COMPUTERNAME = SPCH2008 
wsgi.multiprocess = False 
PROCESSOR_LEVEL = 16 
USERDOMAIN = SPCH2008 
VS100COMNTOOLS = D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\ 
HTTP_ACCEPT_LANGUAGE = zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 
SERVER_PROTOCOL = HTTP/1.1 
SERVER_SOFTWARE = WSGIServer/0.1 Python/2.7.4 
PSMODULEPATH = C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ 
SCRIPT_NAME =  
COMMONPROGRAMFILES = C:\Program Files (x86)\Common Files 
PROCESSOR_IDENTIFIER = AMD64 Family 16 Model 5 Stepping 3, AuthenticAMD 
REQUEST_METHOD = GET 
PROGRAMFILES = C:\Program Files (x86) 
PROCESSOR_REVISION = 0503 
PATH = D:/Program Files (x86)/java/jre7/bin/client;D:/Program Files (x86)/java/jre7/bin;D:/Program Files (x86)/java/jre7/lib/i386;C:\python32\;C:\python32\Lib\site-packages\;C:\python32\Scripts\;C:\Program Files (x86)\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files (x86)\java\jre7\bin;D:\Program Files (x86)\Rational\common;D:\Program Files (x86)\eclipse; 
QUERY_STRING =  
SYSTEMROOT = C:\Windows 
PROGRAMFILES(X86) = C:\Program Files (x86) 
PT5HOME = d:\Program Files (x86)\Cisco Packet Tracer 5.3.3 
CONTENT_LENGTH =  
HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 
HTTP_CONNECTION = keep-alive 
TEMP = C:\Users\spch2008\AppData\Local\Temp 
REMOTE_ADDR = 127.0.0.1 
COMMONPROGRAMFILES(X86) = C:\Program Files (x86)\Common Files 
PROCESSOR_ARCHITECTURE = x86 
wsgi.url_scheme = http 
ALLUSERSPROFILE = C:\ProgramData 
PYDEV_CONSOLE_ENCODING = GBK 
SERVER_PORT = 8080 
LOCALAPPDATA = C:\Users\spch2008\AppData\Local 
HOMEPATH = \Users\spch2008 
USERDOMAIN_ROAMINGPROFILE = SPCH2008 
PROGRAMW6432 = C:\Program Files 
USERNAME = spch2008 
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
LOGONSERVER = \\MicrosoftAccount 
PROMPT = $P$G 
COMSPEC = C:\Windows\system32\cmd.exe 
PROGRAMDATA = C:\ProgramData 
PYTHONPATH = D:\Program Files (x86)\eclipse\plugins\org.python.pydev_2.7.3.2013031601\pysrc\pydev_sitecustomize;E:\GitHub\OpenStack\WSGI;C:\python32\DLLs;C:\python32\lib;C:\python32\lib\plat-win;C:\python32\lib\lib-tk;C:\python32;C:\python32\lib\site-packages 
PATH_INFO = / 
wsgi.multithread = True 
wsgi.input = <socket._fileobject object at 0x0285C030> 
wsgi.errors = <open file '<stderr>', mode 'w' at 0x01DA60D0> 
HTTP_HOST = localhost:8080 
SESSIONNAME = Console 
PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC 
ASL.LOG = Destination=file 
FP_NO_HOST_CHECK = NO 
WINDIR = C:\Windows 
wsgi.file_wrapper = wsgiref.util.FileWrapper 
HTTP_ACCEPT_ENCODING = gzip, deflate 
wsgi.version = (1, 0) 
APPDATA = C:\Users\spch2008\AppData\Roaming 
HOMEDRIVE = C: 
SERVER_NAME = spch2008 
wsgi.run_once = False 
REMOTE_HOST = spch2008 
SYSTEMDRIVE = C: 
GATEWAY_INTERFACE = CGI/1.1 
PYDEV_COMPLETER_PYTHONPATH = D:\Program Files (x86)\eclipse\plugins\org.python.pydev_2.7.3.2013031601\pysrc 
NUMBER_OF_PROCESSORS = 4 
DJANGO_SETTINGS_MODULE = WSGI.settings 
CONTENT_TYPE = text/plain 
PROCESSOR_ARCHITEW6432 = AMD64 
COMMONPROGRAMW6432 = C:\Program Files\Common Files 
OS = Windows_NT 
PUBLIC = C:\Users\Public 
USERPROFILE = C:\Users\spch2008

req = Request(environ)

通过Request操作上述环境变量,所得结果如下:

req.method             'Get'
req.path_info            '/'
req.content_type     'text/plain'
req.remote_user     'None'
req.host                    'localhost:8080'

即通过req,可以很方便的读取environ环境变量,更多操作请看:http://docs.webob.org/en/latest/modules/webob.html


Response

Response包含了所有响应WSGI Server需要的变量。

res = Response()
res.status = 200
res.headerlist = [('Content-type', 'text/html')]
res.body = 'Hello World!'

使用webob改写之前的Hello World程序。j将上述代码粘贴到eclipse中,运行。

from wsgiref.simple_server import make_server
from webob import Request, Response

class MyApp:
    def __call__(self, environ, start_response):
        
        req = Request(environ)
        res = Response()
       
        res.status = 200
        res.headerlist = [('Content-Type', 'text/plain')]
        res.body = "Hello World!"
        
        return res(environ, start_response)
        
application = MyApp()

httpd = make_server('localhost', 8080, application)  
httpd.serve_forever() 

你可能感兴趣的:(Webob Request Response)