Tag Archives: python 3.6 socket tcp

python 3.6 socket tcp Connect TypeError: a bytes-like object is required, not ‘str’

About socket TCP communication code in Python core programming:

Client:

# !/usr/bin/env python

from socket import *

HOST = '*******'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
    data = input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZE)
    if not data:
        break
    print(data.decode('utf-8'))

tcpCliSock.close()

Server:

# !/usr/bin/env python

from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
    print("waiting for connection...")
    tcpCliSock, addr = tcpSerSock.accept()
    print("...connected from: ", addr)

    while True:
        data = tcpCliSock.recv(BUFSIZE)
        if not data:
            break
        print(type(data))
        tcpCliSock.send("[%s] %s" %(ctime(), data))

    tcpCliSock.close()

tcpSerSock.close()

Running in python 3.6 environment reports an error.

Traceback (most recent call last):
File “.\tsTclnt3.py”, line 17, in <module>
tcpCliSock.send(data)
TypeError: a bytes-like object is required, not ‘str’

So change the client code as follows.

—>tcpCliSock.send(data.encode())

Running error reported.

Printed data data type is <class ‘bytes’>, so was curious why this error was reported. Looking up the information online I saw this passage.

In python 3, bytes strings and unicodestrings are now two different types. Since sockets are not aware of string encodings, they are using raw bytes strings, that have a slightly differentinterface from unicode strings.

So, now, whenever you have a unicode stringthat you need to use as a byte string, you need toencode() it. And whenyou have a byte string, you need to decode it to use it as a regular(python 2.x) string.

Unicode strings are quotes enclosedstrings. Bytes strings are b”” enclosed strings

When you use client_socket.send(data),replace it by client_socket.send(data.encode()). When you get datausing data = client_socket.recv(512), replace it by data =client_socket.recv(512).decode()

The code was then modified as follows.

—>

That’s how you get through.

Finally, the ultimate code.

Server side.

# !/usr/bin/env python

from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM) #Create a socket
tcpSerSock.bind(ADDR) #Bind the socket to the address
tcpSerSock.listen(5) #Listen (set maximum number of listeners)

while True:
    print("waiting for connection...")
    tcpCliSock, addr = tcpSerSock.accept()
    print("...connected from: ", addr)

    while True:
        data = tcpCliSock.recv(BUFSIZE).decode()
        if not data:
            break
        tcpCliSock.send(("[%s] %s" %(ctime(), data)).encode())

    tcpCliSock.close()

tcpSerSock.close()

Client:

# !/usr/bin/env python

from socket import *

HOST = '*********'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM) #Create a socket
tcpCliSock.connect(ADDR) # Request to establish a connection

while True:
    data = input('> ')
    if not data:
        break
    tcpCliSock.send(data.encode())
    data = tcpCliSock.recv(BUFSIZE)
    if not data:
        break
    print(data.decode())

tcpCliSock.close()