我用 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”,它就会保持为零。为什么?