ajax是tcp连接吗,如何通过ajax启动/停止twisted TCP连接并获取连接状态

在twisted应用程序中,我想通过ajax POST启动/停止tcp连接(到modbus)。我有一个按钮名为“连接”或“断开连接”,这取决于连接状态。在

现在我的代码看起来像:class ConnectHandler(Resource):

modbus_connection = None

def try_disconnect(self):

log.msg('Disconnecting...')

try:

self.modbus_connection.disconnect()

except:

log.err()

return self.modbus_connection.state

def try_connect(self):

try:

framer = ModbusFramer(ClientDecoder())

reader = DataReader()

factory = ModbusFactory(framer, reader) # inherits from ClientFactory

self.modbus_connection = reactor.connectTCP(ip, 502, factory)

except:

log.err()

return str(self.modbus_connection.state)

def render_POST(self, request):

if self.modbus_connection and \

self.modbus_connection.state == 'connected':

return self.try_disconnect()

else:

return self.try_connect()

现在连接开始时我得到“连接”,停止连接时得到“连接”。

我想等待响应,直到连接建立或取消,并返回连接状态(连接或断开+可选的错误描述)。在

谢谢。在