Search code examples
pythonhttp-headersbottle

How to close the connection


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.


Solution

  • 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.