【问题标题】:why does my python wsgi server function run twice per request?为什么我的 python wsgi 服务器函数每次请求运行两次?
【发布时间】:2020-03-10 11:42:39
【问题描述】:

我用 python3 和 WSGI 模块编写了一个简单的 Web 服务器:

#!/usr/bin/python3

from wsgiref.simple_server import make_server
port = 80
count = 0

def hello_app(environ, start_response):
    global count
    status = '200 OK'  # HTTP Status
    headers = [('Content-type', 'text/plain')]  # HTTP Headers
    start_response(status, headers)
    response = "hello number {}".format(count)
    count += 1
    return( [response.encode()] )


httpd = make_server('', port, hello_app)
print("Serving HTTP on port {}...".format(port))

# Respond to requests until process is killed
httpd.serve_forever()

它工作正常,但每次我从浏览器发出请求时,计数都会增加 2,而不是 1。如果我注释掉“count += 1”,它就会保持为零。为什么?

【问题讨论】:

  • 如果您已经解决了,请发布“答案”而不是在问题中发布解决方案。
  • 谢谢。我现在将在我今天编写代码时调入你的音乐。

标签: python-3.x wsgiserver


【解决方案1】:

问题是一个非常愚蠢的问题 - 浏览器为每个请求自动请求“favicon”文件。通过使用解决方案here 修复。

【讨论】:

    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2021-04-23
    • 2021-06-01
    • 1970-01-01
    • 2014-10-19
    相关资源
    最近更新 更多