How to close the connection

拟墨画扇 提交于 2019-12-12 00:45:14

问题


I'm migrating a simple toolset from python 2.7 to 3.5 and one of the tools is a simple web server using web.py.

Unfortunately web.py is not available for 3.5 yet so I switched to bottle.py for this.

According to the specification of the interface I'm creating I need to close the connection which I can do quite easily in web.py by adding the following line:

web.header('Connection', 'close')

But using bottle I get the error that hop-by-hop headers are not allowed when I do the following:

response.add_header('Connection', 'close')

How do I add this header to the response anyway? I've read the bottle documentation, searched online and looked through the bottle code.


回答1:


I'm not sure so take what I write with a grain of salt.

The bottle development server is a lightly modified wsgiref server which is a sligthly modified http.server server. It has no easy methods or configurations to send "unusual" headers. You can though subclass it and write some custom code. I think it should be enough to overwrite the send_head method (here) and include self.send_header("Connection", "close") somewhere.

You can use bottly with any wsgi server you like. It has inbuild support for quite some servers, but any wsgi server should be able to serve the app. Maybe there is an easier way for other servers to send the custom header.

Also the http.server is not meant for production so you might want to change it even if you can get your header to work. If it is only for internal usage you might get away with using it.



来源:https://stackoverflow.com/questions/38079916/how-to-close-the-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!