uWSGI基础攻略

uWSGI的安装就不多说了、不懂的参考前面的文章、以下例子都是以http的形式来启动、比如莪的py文件名称叫做return_enviorn.py、那么便是这么来执行、如果迩想查看效果、请打开http://hostip:9090来查看

./uwsgi --http :9090 --wsgi-file ../program/py_script/return_enviorn.py

 

 

关于uWSGI最简单的形式如下、其中的environ包含了各种从客户端带来的信息

# This is our application object. It could have any name,
#
except when using mod_wsgi where it must be "application"
def application( # It accepts two arguments:
# environ points to a dictionary containing CGI like environment variables
# which is filled by the server for each received request from the client
environ,
# start_response is a callback function supplied by the server
# which will be used to send the HTTP status and headers to the server
start_response):

# build the response body possibly using the environ dictionary
response_body = 'The request method was %s' % environ['HTTP_USER_AGENT']

# HTTP response code and message
status = '200 OK'

# These are HTTP headers expected by the client.
# They must be wrapped as a list of tupled pairs:
# [(Header name, Header value)].
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]

# Send them to the server using the supplied function
start_response(status, response_headers)

# Return the response body.
# Notice it is wrapped in a list although it could be any iterable.
return [response_body]


假如莪们把environ['HTTP_USER_AGENT']改成environ、则是输出所有信息、比如这样

 

 1 The request method was {
2 'SCRIPT_NAME': '',
3 'REQUEST_METHOD': 'GET',
4 'UWSGI_ROUTER': 'http',
5 'SERVER_PROTOCOL': 'HTTP/1.1',
6 'QUERY_STRING': '',
7 'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)',
8 'HTTP_CONNECTION': 'Keep-Alive',
9 'SERVER_NAME': 'M12-129',
10 'REMOTE_ADDR': '172.16.1.3',
11 'HTTP_THREADID': '4416',
12 'wsgi.url_scheme': 'http',
13 'SERVER_PORT': '9090',
14 'uwsgi.node': 'M12-129',
15 'wsgi.input': <uwsgi._Input object at 0x1a895b0>,
16 'HTTP_HOST': '172.16.3.129:9090',
17 'wsgi.multithread': False,
18 'REQUEST_URI': '/',
19 'HTTP_ACCEPT': 'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*',
20 'wsgi.version': (1, 0),
21 'wsgi.run_once': False,
22 'wsgi.errors': <open file 'wsgi_input', mode 'w' at 0x7f5bf60644b0>,
23 'wsgi.multiprocess': False,
24 'HTTP_ACCEPT_LANGUAGE': 'zh-CN',
25 'uwsgi.version': '0.9.8.6',
26 'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
27 'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
28 'PATH_INFO': '/'
29 }

 可是看出、这个environ里面所包含的信息和CGI的环境变量非常的相似、但又有所不同、比如有包括了UWSGI的路由形式、UWSGI的多线程和多进程情况等等、应该可以说是CGI环境变量的加强版本

 

 

 1 #!/usr/bin/env python
2
3 from wsgiref.simple_server import make_server
4 from cgi import parse_qs, escape
5
6 html = """
7 <html>
8 <body>
9 <form method="get" action="parsing_get.wsgi">
10 <p>
11 Age: <input type="text" name="age">
12 </p>
13 <p>
14 Hobbies:
15 <input name="hobbies" type="checkbox" value="software"> Software
16 <input name="hobbies" type="checkbox" value="tunning"> Auto Tunning
17 </p>
18 <p>
19 <input type="submit" value="Submit">
20 </p>
21 </form>
22 <p>
23 Age: %s<br>
24 Hobbies: %s
25 </p>
26 </body>
27 </html>"""
28
29 def application(environ, start_response):
30
31 # Returns a dictionary containing lists as values.
32 d = parse_qs(environ['QUERY_STRING'])
33
34 # In this idiom you must issue a list containing a default value.
35 age = d.get('age', [''])[0] # Returns the first age value.
36 hobbies = d.get('hobbies', []) # Returns a list of hobbies.
37
38 # Always escape user input to avoid script injection
39 age = escape(age)
40 hobbies = [escape(hobby) for hobby in hobbies]
41
42 response_body = html % (age or 'Empty',
43 ', '.join(hobbies or ['No Hobbies']))
44
45 status = '200 OK'
46
47 # Now content type is text/html
48 response_headers = [('Content-Type', 'text/html'),
49 ('Content-Length', str(len(response_body)))]
50 start_response(status, response_headers)
51
52 return [response_body]
53
54 httpd = make_server('localhost', 8051, application)
55 # Now it is serve_forever() in instead of handle_request().
56 # In Windows you can kill it in the Task Manager (python.exe).
57 # In Linux a Ctrl-C will do it.
58 httpd.serve_forever()

 

 

 其中第32行的函数parse_qs()是个解析网页请求参数为数组的冬冬、具体的用法可以看下面的这个例子

1 import urlparse
2 qsdata = "test=test&test2=test2&test2=test3"
3 qs = urlparse.parse_qs(qsdata)
4 print qs

输出:

{'test': ['test'], 'test2': ['test2', 'test3']}

 

posted @ 2011-12-14 10:04  klobodnf  阅读(2494)  评论(0编辑  收藏  举报