在扭曲的应用程序中,我想通过ajax POST启动/停止tcp连接(到modbus)。根据连接状态,我有一个标题为连接或断开连接的按钮。如何通过ajax启动/停止扭曲的TCP连接并获取连接状态
现在我的代码如下所示:
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()
现在我得到“连接”时,连接开始和“连接”停止连接时。 我想等待回应,直到连接建立或废除并返回连接状态(连接或断开连接+可选的错误描述)。
谢谢。
2012-08-24
marcinpz