Skip to content

Instantly share code, notes, and snippets.

@ardenn
Created November 1, 2018 14:37
Show Gist options
  • Save ardenn/9419a2570c58f557d9926b9d25afc084 to your computer and use it in GitHub Desktop.
Save ardenn/9419a2570c58f557d9926b9d25afc084 to your computer and use it in GitHub Desktop.
A socket client that receives binary data and unpacks them
import binascii
import socket
import struct
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 10000)
sock.bind(server_address)
sock.listen(1)
unpacker = struct.Struct('I 2s f')
while True:
print('\nwaiting for a connection')
connection, client_address = sock.accept()
try:
data = connection.recv(unpacker.size)
print('received {!r}'.format(binascii.hexlify(data)))
unpacked_data = unpacker.unpack(data)
print('unpacked:', unpacked_data)
finally:
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment