Created
November 1, 2018 14:37
-
-
Save ardenn/9419a2570c58f557d9926b9d25afc084 to your computer and use it in GitHub Desktop.
A socket client that receives binary data and unpacks them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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