sending socket data as hex?



  • Hi,

    with send() I can only send strings, right? But I want to send hex data, how can I do this?

    My sample code:

    import socket

    host = '192.168.4.2' # laptop running server
    port = 2004
    addr = socket.getaddrinfo(host, port)[0][-1]
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(addr)
    s.send('ffff') <- at this point I don't want to send ffff as text but as hex
    s.close()


  • Pybytes Beta

    @horst

    You should use binary data.

    For binary use the following format.
    s.send(b.'ffff')

    For hex:

    import binascii
    s.send(binascii.hexlify(b'ffff'))

    Or do you want this?

    s.send(int('0xffff', 16))


Log in to reply
 

Pycom on Twitter