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()
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.