2

Am working on the back-end of Django web app that also has to do socket I/O with sensor devices in the field via GPRS using TCP communication.

I'd managed to succeed in obtaining data from the devices using the python module asyncore. What I've been doing was configuring the devices to send data via TCP to the server, and then I would pick up this incoming data using asyncore (which I chose because of it's asynchronous approach to socket i/o).

The docs talk of overloading the asyncore.dispatcher or asyncore.dispatcher_with_send in order to be able to implement custom actions in the callbacks like handle_write, handle_read, etc.

Currently, am able to obtaining incoming data by picking it up in the handle_read method, but a new requirement came up recently:

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.

So, I'd added code in the handle_write method in my implementation, code to send the "identify yourself" commands to the device that has connected to us. But my problem is that this code never executes even when devices continue to connect and the handle_read shows I can obtain the incoming data!

I tried putting the "identify yourself" code (that sends some hex commands like below)

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')

hoping that a response would then come in via handle_read while I hold onto the data that was sent previously, but this fails! What can I do?

What is the better /best way to pull this off?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
JWL
  • 13,591
  • 7
  • 57
  • 63
  • 1
    I can't give you a full answer, but in case you weren't aware of it, you might want to look at the [Twisted framework](http://twistedmatrix.com/) for work like this :) – detly Jun 21 '11 at 11:29
  • Please paste the code you are currently using, you probably want to buffer the data you need to send. – Florian Mayer Jun 27 '11 at 09:44

1 Answers1

0

If you are implementing a protocol which has some commands and some data exchange, then it is better to use the asynchat library along with asyncore.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106