Hi,

I want to develop server application in python. GPS device sends IMEI number to server and then server has to reply to device 0x01. Server must send answer – 1 Byte in HEX format. when device receives from server 0x01 it keeps connection and sends other data. if number is not 0x01, device disconnects from server.

with Qt I realized this server like this

Code:
void Network::ServerStart()
{
     Server.listen(QHostAddress::Any, 2342);
}
void Network::ServerAcceptConnection()
{
    clientConnection = Server.nextPendingConnection();
   qDebug() << clientConnection->peerAddress().toString();

   connect(clientConnection, SIGNAL(readyRead()),
   this, SLOT(read()));
}

void Network::read()
{

      QByteArray buffer;
      qDebug() << "avaliable on start" << clientConnection->bytesAvailable();
      buffer = clientConnection->readAll();
      qDebug() << "read to buffer" << buffer.toHex();
      qDebug() << "availiable now" << clientConnection->bytesAvailable();

      if (buffer.count()< 30){
                       QByteArray ba;
                          QDataStream out(&ba, QIODevice::WriteOnly);
                          out << true;

                           clientConnection->write(ba);
                      }else
      {
.......... work with data
}
}
in python I'm trying to achieve similar, I want to sent true value to device. when device gets this true value it sends GPS locations.

Code:
 import SocketServer
import binascii
from struct import *

class MyTCPHandler(SocketServer.BaseRequestHandler):


    def handle(self):
     
        self.data = self.request.recv(4000).strip()
        print "%s wrote:" % self.client_address[0]
        print self.data
    
        self.request.send('1')
        self.data = self.request.recv(4000).strip()
        print "%s wrote:" % self.client_address[0]
        print self.data
        

if __name__ == "__main__":
    HOST, PORT = '', 2342

  
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    server.serve_forever()
I think I'm sending wrong value, I heave also tried these options:

self.request.send(0b01)
self.request.send('1')
self.request.send('01')
self.request.send(pack ('b',1))
binascii.a2b_qp("1", header=False)
binascii.a2b_qp("01", header=False)

non of that worked. I get only IMEI number but after that device disconnects from python server and sends IMEI again. But Qt code works. I'm not good in binary programing.

Thanks for advice.