L
L
lavrenkov-sketch2020-11-03 14:48:38
Python
lavrenkov-sketch, 2020-11-03 14:48:38

How to decode a string of bytes in python?

Initially, the socket sends to python

14:46:37:820 отправлено: 33 байт
 7e 41 01 00 00 00 00 00 10 1d 43 a1 5f 01 1d 43
 a1 5f 30 c4 55 01 f0 a8 fb 01 00 00 00 00 00 00 e4

~A  Size = 1
numPage = 0  Code = 4096  Time = 1604403997  StateGauge = 1  LastTime = 1604403997  Lat = 22398000  Lon = 33270000  Speed = 0  Course = 0

The python itself comes
b'~A\x01\x00\x00\x00\x00\x00\x10\x1dC\xa1_\x01\x1dC\xa1_0\xc4U\x01\xf0\xa8\xfb\x01\x00\x00\x00\x00\x00\x00\xe4'


Python code:
spoiler
import socket
import re

# Задаем адрес сервера
SERVER_ADDRESS = ('localhost', 8686)

# Настраиваем сокет
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(SERVER_ADDRESS)
server_socket.listen(10)
print('server is running, please, press ctrl+c to stop')

# Слушаем запросы
# Особенности протокола
while True:
    connection, address = server_socket.accept()
    print("new connection from {address}".format(address=address))
# Соединение всегда инициализирует навигатор, через handshake  мы должны ему сразу ответить!
#	0x40, 0x4e, 0x54, 0x43, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x45, 0x5e,0x2a, 0x3c, 0x53,
#    connection.send(bytes('@NTC\x40\x4e\x54\x43\x00\x00\x00\x01\x00\x00\x00\x03\x00\x45\x5e*<S', encoding='UTF-8'))

#Согласование протокола  FLEX v1
#protocol answer
#0x40, 0x4e, 0x54, 0x43,0x00,0x00,0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09, 0x00, 0xb1, 0xa0, 0x2a, 0x3c, 0x46, 0x4c, 0x45, 0x58, 0xb0, 0x0a, 0x0a,
#    connection.send(bytes('x40\x4e\x54\x43\x00\x00\x00\x00\x00\x01\x00\x00\x00\x09\x00\xb1\xa0\x2a\x3c\x46\x4c\x45\x58\xb0\x0a\x0a', encoding='UTF-8'))
    
    while 1:
        data = connection.recv(8192)
        print(data)
        results = [
            dict(re.findall(r"(\d{4})([\d.]+)", string))
            for string in filter(
            bool,  # non-empty
            data.decode('ascii', 'ignore').split("&&"),
        )
        ]
        print(data.decode('ascii', 'ignore'))
        print(data.decode('cp866'))


What is my error, or is it some wrong bytes?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-11-03
@lavrenkov-sketch

Use struct.unpack() it's the best friend of all reverse engineers.

a = b'\x7e\x41\x01\x00\x00\x00\x00\x00\x10\x1d\x43\xa1\x5f\x01\x1d\x43\xa1\x5f\x30\xc4\x55\x01\xf0\xa8\xfb\x01\x00\x00\x00\x00\x00\x00\xe4'
Size,numPage,Code,Time,StateGauge,LastTime,Lat,Lon,Speed,Course, = struct.unpack("<2xBIHIBIIIHH",a[:30])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question