如何关闭连接 [英] How to close the connection

查看:132
本文介绍了如何关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个简单的工具集从python 2.7迁移到3.5,其中一个工具是使用web.py的简单Web服务器。

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.

不幸的是,web.py是

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

根据我正在创建的接口的规范,我需要关闭连接通过添加以下行,可以非常轻松地在web.py中进行操作:

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.

推荐答案

我不确定该如何处理我用一粒盐写。

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

瓶子开发服务器是经过轻微修改的wsgiref服务器,它是经过严格修改的http.server服务器。它没有简单的方法或配置来发送异常标头。您可以将其子类化并编写一些自定义代码。我认为应该足以覆盖send_head方法(此处),并在某处包含 self.send_header( Connection, close)

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.

您可以与您喜欢的任何wsgi服务器一起使用。它具有对某些服务器的内置支持,但是任何wsgi服务器都应该能够为该应用程序提供服务。也许其他服务器可以使用一种更简单的方法来发送自定义标头。

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.

此外,http.server并非用于生产环境,因此即使您愿意也要更改它。可以让您的标题起作用。如果仅用于内部使用,则可能会放弃使用它。

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.

这篇关于如何关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆