0

I tried to send a hexcode string using the socket module in python. I wrote the following:

import socket
HOST = '192.168.140.2' 
PORT = 55000 
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))   
  while True: 
      DATA = '\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff   \x00\xff\x00\xff\x00\xff\x00'
      s.sendall(DATA) 
      data = s.recv(4096)
      s.close()

But it shows this error:

Traceback (most recent call last):
File "E:\Python\echo-client.py", line 9, in <module>
    s.sendall(DATA)
TypeError: a bytes-like object is required, not 'str'

When I send a single hexcode, using s.sendall(b'\xff'), it works.

How can I send a hex string to a server?

Seth
  • 2,214
  • 1
  • 7
  • 21

1 Answers1

0

You forgot the b before the string in the DATA variable. It is required to make it a bytes object instead of a str object, which is accepted by socket.socket.sendall().

Seth
  • 2,214
  • 1
  • 7
  • 21