Python 如何检测服务器关闭unix域套接字?
我正在摆弄python twisted库,我似乎不知道如何让我的客户机检测到服务器关闭其套接字。我的客户端继续让我向不存在的服务器发送数据。这是我的服务器: test_server.py 它所做的只是将收到的每一行发送回连接的客户端。 以下是客户: test_client.py 如果我先运行服务器,然后运行客户机,然后杀死服务器,客户机仍然会写入Python 如何检测服务器关闭unix域套接字?,python,sockets,twisted,unix-socket,Python,Sockets,Twisted,Unix Socket,我正在摆弄python twisted库,我似乎不知道如何让我的客户机检测到服务器关闭其套接字。我的客户端继续让我向不存在的服务器发送数据。这是我的服务器: test_server.py 它所做的只是将收到的每一行发送回连接的客户端。 以下是客户: test_client.py 如果我先运行服务器,然后运行客户机,然后杀死服务器,客户机仍然会写入CommandProtocol的传输,就像什么都没发生一样。我认为errback函数至少会报告一些东西。在客户机在服务器之前运行的情况下,会使用Conn
CommandProtocol
的传输,就像什么都没发生一样。我认为errback
函数至少会报告一些东西。在客户机在服务器之前运行的情况下,会使用ConnectError调用errback
函数,但我特别希望检测客户机已经连接到服务器并且服务器退出的情况
如何检测服务器已关闭?我想os.stat方法和stat模块可以帮助您。您能否将客户端代码更改为如下所示:
import os
import stat
from twisted.internet import protocol, reactor, endpoints, stdio
from twisted.protocols.basic import LineReceiver
class CommandProtocol(protocol.Protocol):
def dataReceived(self, data):
print data,
class StdinProtocol(LineReceiver):
from os import linesep as delimiter
def __init__(self, client):
self._client = client
def connectionMade(self):
self.transport.write('>>> ')
def lineReceived(self, line):
print 'writing line: %s' % line
self._client.transport.write(line + os.linesep)
def printError(failure):
print str(failure)
def main():
if os.stat('./test.sock').st_mode & (stat.S_IRGRP | stat.S_IRUSR | stat.S_IROTH):
print "1"
print "using control socket"
point = endpoints.UNIXClientEndpoint(reactor, './test.sock')
proto = CommandProtocol()
d = endpoints.connectProtocol(point, proto)
d.addErrback(printError)
stdio.StandardIO(StdinProtocol(proto))
reactor.run()
else:
print "not ready"
main()
ITransport.write
在已断开连接的传输上调用时,是无声的no-op
如果要了解连接已丢失,请重写
connectionLost
方法。一旦知道连接已断开,您可以安排程序停止接受输入或对接收到的输入执行其他操作。这将处理客户端在服务器之前启动的情况,但在客户端已连接后服务器关闭的情况下,这没有帮助。我编辑了我的问题以澄清这一点。
import os
from twisted.internet import protocol, reactor, endpoints, stdio
from twisted.protocols.basic import LineReceiver
class CommandProtocol(protocol.Protocol):
def dataReceived(self, data):
print data,
class StdinProtocol(LineReceiver):
from os import linesep as delimiter
def __init__(self, client):
self._client = client
def connectionMade(self):
self.transport.write('>>> ')
def lineReceived(self, line):
print 'writing line: %s' % line
self._client.transport.write(line + os.linesep)
def printError(failure):
print str(failure)
def main():
point = endpoints.UNIXClientEndpoint(reactor, './test.sock')
proto = CommandProtocol()
d = endpoints.connectProtocol(point, proto)
d.addErrback(printError)
stdio.StandardIO(StdinProtocol(proto))
reactor.run()
main()
import os
import stat
from twisted.internet import protocol, reactor, endpoints, stdio
from twisted.protocols.basic import LineReceiver
class CommandProtocol(protocol.Protocol):
def dataReceived(self, data):
print data,
class StdinProtocol(LineReceiver):
from os import linesep as delimiter
def __init__(self, client):
self._client = client
def connectionMade(self):
self.transport.write('>>> ')
def lineReceived(self, line):
print 'writing line: %s' % line
self._client.transport.write(line + os.linesep)
def printError(failure):
print str(failure)
def main():
if os.stat('./test.sock').st_mode & (stat.S_IRGRP | stat.S_IRUSR | stat.S_IROTH):
print "1"
print "using control socket"
point = endpoints.UNIXClientEndpoint(reactor, './test.sock')
proto = CommandProtocol()
d = endpoints.connectProtocol(point, proto)
d.addErrback(printError)
stdio.StandardIO(StdinProtocol(proto))
reactor.run()
else:
print "not ready"
main()