SoFunction
Updated on 2025-03-10

Sample code for codec hexadecimal code streams in Python

python codec hexadecimal code stream

Currently working on python + vue writing websites, I recently encountered a code stream that uses python code to code hexadecimal code to interact with C language programs

I wrote a simple version of the python code for the client and server side and used the struct socket library. Because it uses udp, I simply called the library.

Server code

import socket
import struct

# Create UDP socketserver_socket = (socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 9999)

# Bind the address and portserver_socket.bind(server_address)

while True:
    print('Waiting for data...')
    data, client_address = server_socket.recvfrom(1024)
    
    # Print the hexadecimal code stream of the received original data    print(f'Received raw data: {()}')
    
    # parse the received data    name, age = ('BB', data)  # 'BB' means two unsigned chars    
    # Modify data    name = (name + 1) % 256
    age = (age + 1) % 256
    
    # Repackage data    modified_data = ('BB', name, age)
    
    # Print the hexadecimal code stream of modified data    print(f'Modified data: {modified_data.hex()}')
    
    # Send the modified data back to the client    server_socket.sendto(modified_data, client_address)

Client Code

import socket
import struct

# Create UDP socketclient_socket = (socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 9999)

# Build data structure and packagename = 100
age = 25
data = ('BB', name, age)

# Print the hexadecimal code stream of the sent dataprint(f'Sending data: {()}')

# Send dataclient_socket.sendto(data, server_address)

# Receive server responsemodified_data, server_address = client_socket.recvfrom(1024)

# Print the hexadecimal code stream of data responded to by the serverprint(f'Received modified data: {modified_data.hex()}')

# parse the data responded by the servermodified_name, modified_age = ('BB', modified_data)

print(f'Modified Name: {modified_name}')
print(f'Modified Age: {modified_age}')

# Close the socketclient_socket.close()

Timer (apscheduler) sends regularly

After adding the timer, the server code is the same. I set the client code to send it once every 10 seconds.

The code is as follows:

import socket
import struct
from  import BackgroundScheduler

def send_data():
    # Build data structure and package    name = 100
    age = 25
    data = ('BB', name, age)
    
    # Print the hexadecimal code stream of the sent data    print(f'Sending data: {()}')
    
    # Send data    client_socket.sendto(data, server_address)

# Create UDP socketclient_socket = (socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 9999)

# Create a background schedulerscheduler = BackgroundScheduler()
()

# Add a task that sends data regularly, and executes it every 10 secondsscheduler.add_job(send_data, 'interval', seconds=10)

try:
    # Block the main thread and wait for the task to be executed    while True:
        pass
except KeyboardInterrupt:
    # Capture Ctrl+C signals, stop the scheduler and close the socket    ()
    client_socket.close()

This is the article about the example code of Python codec hexadecimal streaming. For more related content of Python codec hexadecimal streaming, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!