扭曲的handle_quit()方式断开连接?
所以我正在使用twisted(python-loggingserver)实现一个日志服务器,并向服务器添加了简单的身份验证。如果身份验证失败,我想关闭与客户端的连接。日志服务器代码中的类已经有一个名为handle_quit()的函数。这是关闭连接的正确方法吗?这是一个代码片段:
if password != log_password:
self._logger.warning("Authentication failed. Connection closed.")
self.handle_quit()
so I'm implementing a log server with twisted (python-loggingserver) and I added simple authentication to the server. If the authentication fails, I wanna close the connection to the client. The class in the log server code already has a function called handle_quit(). Is that the right way to close the connection? Here's a code snippet:
if password != log_password:
self._logger.warning("Authentication failed. Connection closed.")
self.handle_quit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您引用的
handle_quit
消息是 这个,那么应该可以正常工作。该方法所做的唯一事情是self.transport.loseConnection()
,它关闭连接。您也可以自己执行 self.transport.loseConnection() ,这将完成相同的事情(因为它当然是相同的事情)。我会在这两个选项之间进行选择,考虑失败的身份验证是否应该只是关闭连接,或者是否应该始终以与退出命令相同的方式对待它。在当前代码中,这没有什么区别,但您可能会想象 quit 命令在将来的某个时刻有额外的处理(清理一些资源或其他东西)。If the
handle_quit
message you're referring to is this one, then that should work fine. The only thing the method does isself.transport.loseConnection()
, which closes the connection. You could also just doself.transport.loseConnection()
yourself, which will accomplish the same thing (since it is, of course, the same thing). I would select between these two options by thinking about whether failed authentication should just close the connection or if it should always be treated the same way a quit command is treated. In the current code this makes no difference, but you might imagine the quit command having extra processing at some future point (cleaning up some resources or something).