首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何调试/记录wsgi python应用程序?

如何调试/记录wsgi python应用程序?
EN

Stack Overflow用户
提问于 2012-07-03 19:20:45
回答 1查看 23.6K关注 0票数 6

我试过这个:

代码语言:javascript
复制
#!/usr/bin/python    

from wsgiref.simple_server import make_server
from cgi import parse_qs, escape
import logging
import os
import sys

html = """
<html>
<body>
   <form method="post" action="parsing_post.wsgi">
      <p>
         Age: <input type="text" name="age">
         </p>
      <p>
         Hobbies:
         <input name="hobbies" type="checkbox" value="software"> Software
         <input name="hobbies" type="checkbox" value="tunning"> Auto Tunning
         </p>
      <p>
         <input type="submit" value="Submit">
         </p>
      </form>
   <p>
      Age: %s<br>
      Hobbies: %s
      </p>
   </body>
</html>
"""

def application(environ, start_response):

   # the environment variable CONTENT_LENGTH may be empty or missing
   try:
      request_body_size = int(environ.get('CONTENT_LENGTH', 0))
   except (ValueError):
      request_body_size = 0

   # When the method is POST the query string will be sent
   # in the HTTP request body which is passed by the WSGI server
   # in the file like wsgi.input environment variable.
   logger = logging.getLogger(__name__)
   request_body = environ['wsgi.input'].read(request_body_size)
   d = parse_qs(request_body)


   age = d.get('age', [''])[0] # Returns the first age value.
   hobbies = d.get('hobbies', []) # Returns a list of hobbies.

   # Always escape user input to avoid script injection
   age = escape(age)
   hobbies = [escape(hobby) for hobby in hobbies]

   response_body = html % (age or 'Empty',
               ', '.join(hobbies or ['No Hobbies']))

   status = '200 OK'

   response_headers = [('Content-Type', 'text/html'),
                  ('Content-Length', str(len(response_body)))]
   start_response(status, response_headers)

   return [response_body]

但我不知道它记录在哪里。我正在尝试在网页上或文件/var/log/apache2/myapp.log中显示/记录值

做这件事最好的方法是什么?

任何答案都将受到高度赞赏。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-03 19:34:48

请注意,上面的代码实际上不会生成任何日志,因为您没有调用任何logger.log()变体-但我猜这不是重点。

如果你使用apache/mod_wsgi运行你的代码,最简单的解决方案是配置你的记录器使用StreamHandler (cf http://docs.python.org/howto/logging.html#configuring-logging)记录到sys.stderr,并在你的apache conf中定义错误日志路径、名称和级别(注意,默认的apache行为是只记录“错误级别”消息)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11309840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档