0

I used python twisted and receiving data properly only sometimes I am receiving data with hex dat like this:

b'\x05\x00\x00\x00\xe9\x00{ "iMsgType": 5, "l_szSessionId": "Ranjith;11961"}\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Could anyone please give me solution? Earlier I used socket programming but missing data, but twisted is good I am getting this data.

Client code is in c sending json string through socket.

Please find the below python code

from twisted.internet import protocol, reactor

class EchoProtocol(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)  # Echo back the received data

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return EchoProtocol()

if __name__ == "__main__":
    port = 12345  # Specify the port you want to listen on
    reactor.listenTCP(port, EchoFactory())
    print(f"Listening on port {port}")
    reactor.run()


toyota Supra
  • 3,181
  • 4
  • 15
  • 19

1 Answers1

0

Your program is receiving the bytes that the program holding the other end of the socket is sending.

The example data you show clearly contains some bytes that are the result of JSON serialization but there are other bytes there which don't appear to be part of valid JSON-encoded data.

This suggests the other program isn't sending just JSON-encoded data but something else. Perhaps it is framing that data using some protocol.

You will have to understand the protocol that program is using and then implement appropriate interpretation in your Twisted/Python-based program to make sense of the bytes you receive.

One thing you should note is that b'\x05\x00\x00\x00\xe9\x00... is not "hex data". It is Python's representation of a byte string containing non-printable bytes - and that representation uses hex representation for the integer values of those non-printable bytes.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • the client code is in c and it is sending json_string using socket programming to python twisted. – RANJEETH NEDUNURI Aug 18 '23 at 11:12
  • Unfortunately that's not enough detail. You need to know what it is doing *specifically*. From what your Python program is showing us, it is not *only* sending the JSON. You need to know what all of the bytes it is sending mean. Currently you only know what some of them mean. Since I do not have the C program in question, there is no way for me to know what they mean. – Jean-Paul Calderone Aug 19 '23 at 15:11