3

The API for Python's wsgiref module precludes hop-by-hop headers (as defined in RFC 2616).

I'm unclear on how to get the server to terminate a connection after a response (since there doesn't seem to be a way to add Connection: close).

This problem comes up in testing small WSGI apps and Bottle micro-services. Calls from curl get blocked by open connections from a browser. I have to click a browser refresh to terminate the connection so that the pending curl request can be answers.

Obviously, this should be a server side decision (terminate connection after a response) rather than client-side. I'm unclear how to implement this.

4

1 回答 1

0

这实际上取决于您托管框架的 WSGI 服务器。使用瓶子的最佳解决方案是通过 gevent 运行它。

botapp = bottle.app()
for Route in (mainappRoute,): #handle multiple files containing routes
    botapp.merge(Route)
botapp = SessionMiddleware(botapp, beakerconfig) #in case you are using beaker sessions
botapp = WhiteNoise(botapp) #in case you want whitenoise to handle static files
botapp.add_files(staticfolder, prefix='static/') #add static route to whitenoise
server = WSGIServer(("0.0.0.0", int(80)), botapp) #gevent async web server
def shutdown():
    print('Shutting down ...')
    server.stop(timeout=60)
    exit(signal.SIGTERM)
gevent.signal(signal.SIGTERM, shutdown)
gevent.signal(signal.SIGINT, shutdown) #CTRL C
server.serve_forever() #spawn the server

如果不需要,您可以清除白噪声和瓶子配置,我将它们放在那里作为示例,并建议您在朝外时使用它们。

这在每个连接上都是完全异步的。

于 2018-12-27T00:40:41.720 回答