Home > front end >  TypeError: a bytes-like object is required not 'str'
TypeError: a bytes-like object is required not 'str'

Time:10-03

import socket 
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("192.168.152.140", 4444))


connection.send("\n[ ] connection established.\n")

connection.close()

CodePudding user response:

Try this

connection.send("\n[ ] connection established.\n".encode())

This should work

CodePudding user response:

Your connection.send argument needs to be bytes, but you're passing a string. You should encode it before sending e.g. message.encode('utf-8')

message = "\n[ ] connection established.\n";

connection.send(message.encode('utf-8'));

CodePudding user response:

The socket.send function expects bytes, not a string (docs)

You need to convert the string to bytes before sending: https://www.askpython.com/python/string/python-string-bytes-conversion

It should probably look like this:

connection.send("\n[ ] connection established.\n".encode("utf-8"))
  • Related