Created
November 1, 2018 14:38
-
-
Save ardenn/a906bc6f98d11a91089bd77cfc8f81e7 to your computer and use it in GitHub Desktop.
A socket that sends binary data
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.connect(server_address) | |
values = (1, b'ab', 2.7) | |
packer = struct.Struct('I 2s f') | |
packed_data = packer.pack(*values) | |
print('values =', values) | |
try: | |
# Send data | |
print('sending {!r}'.format(binascii.hexlify(packed_data))) | |
sock.sendall(packed_data) | |
finally: | |
print('closing socket') | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment