我在Django web应用程序的后端工作,该应用程序还必须通过GPRS使用TCP通信与现场传感器设备进行socket I/O。在
我成功地使用python模块asyncore从设备获取数据。我一直在做的是将设备配置为通过TCP向服务器发送数据,然后我将使用asyncore(我选择它是因为它是异步的socket I/o方法)来接收这些传入的数据。在
文档讨论重载asyncore.dispatcher或asyncore.dispatcher_with_send,以便能够在回调中实现自定义操作,如handle_write,handle_read,等等
目前,我可以通过handle_read方法获取传入数据,但最近出现了一个新的要求:It is required to be able to hold onto the connection when a device connects to the server, so that we can send to it some commands (say like requiring it to identify itself), and thereafter continue to use it's sent data once found genuine.
因此,我在我的实现中添加了handle_write方法中的代码,用于将“identify yourself”命令发送到连接到我们的设备。但我的问题是,即使设备继续连接并且handle_read显示我可以获得传入的数据,这段代码也永远不会执行!在
我试着输入“识别你自己”的代码(它发送一些十六进制的命令,如下所示)print 'Attempting to Identify Remote Party...'
self.send('\x40\x40\x00\x11\x12\x34\x56\xFF\xFF\xFF\xFF\x90\x01\x41\xCF\x0D\x0A')
希望在我保留之前发送的数据时,通过handle_read来响应,但这失败了!我能做什么?在
有什么更好的/最好的方法来完成这个任务?在