1

I'm trying to connect to a server using socket module in python. However, I get an error message as shown below.

TypeError: a bytes-like object is required, not 'str'

Here's my code:

import socket
HOST = '0.0.0.0'
PORT = 12345

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))


server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")


client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")


while True:
    try:
        command = input(client_ip+ ">")
        if(len(command.split()) != 0):
            client_socket.send(command)

        else:
            continue

    except(EOFError):
        print("Invalid input, type 'help' to get a list of implented commands. \n")

        continue

    if(command == "quit"):
        break


    data = client_socket.recv(1024)
    print(data + "\n")


client_socket.close()

What causes this TypeError? and what should I do to fix this ?. I'm also open to any advice to improve my python socket code writing.

Willy satrio nugroho
  • 908
  • 1
  • 16
  • 27
silent whatefver
  • 13
  • 1
  • 1
  • 3
  • 1
    When you say 'better' how exactly do you mean? Could you also post more of your error log to identify which line is having the problem. I'd assume when you type client_socket.send(command) is your issue for the bytes like object, try client_socket.send(bytes(command)) – Liam Aug 29 '20 at 19:02
  • 1
    Does this answer your question? [Best way to convert string to bytes in Python 3?](https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – sushanth Aug 29 '20 at 19:05
  • So now its TypeError: string argument without an encoding – silent whatefver Aug 29 '20 at 19:25

3 Answers3

0

easily, just convert the string to a byte-wise object with

string = "example string"
byte_wise_string = string.encode("utf-8")

Hope I could help

Greets

Ari24
  • 370
  • 5
  • 12
0

https://docs.python.org/3/library/socket.html?highlight=socket#socket.socket.send

You'd want to

  • encode the string to byte object before sending: client_socket.send(command.encode())
  • and decode back to string when you concatenate in print: print(data.decode() + "\n")
yibo
  • 517
  • 3
  • 10
0

This error is because python expects bytes not string to be sent via socket.
Hence you have to convert strings to bytes before sending that. you can use encode() to convert string to bytes and use decode() to convert bytes received into string.
I have updated your code, Please refer below, this should work fine.

import socket
HOST = '0.0.0.0'
PORT = 12345

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))


server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")


client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")


while True:
    try:
        command = input(client_ip+ ">")
        if(len(command.split()) != 0):
            client_socket.send(command.encode('utf-8')) #Encoding required here

        else:
            continue

    except(EOFError):
        print("Invalid input, type 'help' to get a list of implented commands. \n")

        continue

    if(command == "quit"):
        break


    data = client_socket.recv(1024)
    print(data.decode('utf-8') + "\n") #Decoding required here


client_socket.close()
Liju
  • 2,273
  • 3
  • 6
  • 21