Python Socket TypeError: a bytes-like object is required, not 'str' 错误提示

When learning socket programming encountered an error code is returned as follows: TypeError: a bytes-like object  is required, not 'str'
find here python3.5 Python2.7 socket and returns the decoded value are different. 

First clear the default encoding is in python3 in unicode. unicode into utf-32 (4 bytes), utf-16 (two bytes), utf-8 (representing 1-4 bytes), so utf-16 is now the most commonly used version unicode. However, taking into utf8 save space, save the file or utf8.

The book example

Server

. 1  # Coding = UTF-. 8 
2  # Create a TCP server 
. 3 from Socket Import *
 . 4  from Time Import the ctime
 . 5  
. 6 the HOST = ''
 . 7 PORT = 21567
 . 8 BUFSIZ = 1024
 . 9 ADDR = (the HOST, PORT)
 10  
. 11 tcpSerSock = Socket ( AF_INET, SOCK_STREAM) # create server socket 
12 tcpSerSock.bind (ADDR) # socket with address binding 
13 tcpSerSock.listen (5)   # listen for connections, the maximum number of incoming connection requests 
14  
15  the while  True :
 16      Print ( 'Waiting for connection...')
17     tcpCliSock,addr =tcpSerSock.accept()
18     print('...connected from:',addr)
19 
20     while True:
21         data =tcpCliSock.recv(BUFSIZ)
22         #print('date=',data)
23         if not data:
24             break
25         tcpCliSock.send(('[%s] %s' %(ctime(),data)))
26 
27     tcpCliSock.close()
28 tcpSerSock.close()

Client

 1 #coding=utf-8
 2 
 3 from socket import *
 4 
 5 HOST = 'localhost' #  or 'localhost'
 6 PORT = 21567
 7 BUFSIZ = 1024
 8 ADDR=(HOST,PORT)
 9 
10 tcpCliSock = socket(AF_INET,SOCK_STREAM)
11 tcpCliSock.connect(ADDR)
12 
13 while True:
14     data = input('> ')
15     print('data=',data);
16     if not data:
17         break
18     tcpCliSock.send(data)
19     data = tcpCliSock.recv(BUFSIZ)
20     if not data:
21         break
22     print(data)
23 
24 tcpCliSock.close() 

Error message is returned: 
TypeError: A bytes-like Object IS required, not 'str' 

When entering information, the client input string (str type), while the code is needed is the type of bytes, with questions to investigate the cause and eventually found on StackOverflow it was also the same problem, and called the Scharron who gives the answer:  

    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 string that you need to use as a byte string, you need to encode() 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 being 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()

Accordingly, it is necessary to decode the type str type conversion bytes for transmission to the server after receiving the information, the need for coding the transmission information when the client terminal, and then converted into bytes Type Type str

The following is the revised code:

Server

. 1  # Coding = UTF-. 8 
2  # Create a TCP server 
. 3  from Socket Import *
 . 4  from Time Import the ctime
 . 5  
. 6 the HOST = '' 
. 7 PORT = 21567
 . 8 BUFSIZ = 1024
 . 9 ADDR = (the HOST, PORT)
 10  
. 11 tcpSerSock = Socket ( AF_INET, SOCK_STREAM) # create server socket 
12 tcpSerSock.bind (ADDR) # socket with address binding 
13 tcpSerSock.listen (5)   # listen for connections, the maximum number of incoming connection requests 
14  
15  the while True:
16     print('waiting for connection...')
17     tcpCliSock,addr =tcpSerSock.accept()
18     print('...connected from:',addr)
19 
20     while True:
21         data =tcpCliSock.recv(BUFSIZ).decode()
22         print('date=',data)
23         if not data:
24             break
25         tcpCliSock.send(('[%s] %s' %(ctime(),data)).encode())
26 
27     tcpCliSock.close()
28 tcpSerSock.close()

Client

# Coding = UTF-8 
# Create a TCP server 
from socket Import *
 from Time Import ctime 

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

tcpSerSock = socket (AF_INET, SOCK_STREAM) # create server socket 
tcpSerSock .bind (aDDR) # socket with address binding 
tcpSerSock.listen (5)   # listen for connections, the maximum number of incoming connection requests 

the while True:
     Print ( ' Waiting for connection ... ' ) 
    tcpCliSock, addr =tcpSerSock.accept()
    print('...connected from:',addr)

    while True:
        data =tcpCliSock.recv(BUFSIZ).decode()
        print('date=',data)
        if not data:
            break
        tcpCliSock.send(('[%s] %s' %(ctime(),data)).encode())

    tcpCliSock.close()
tcpSerSock.close()

Welcome to correct me questions

Guess you like

Origin www.cnblogs.com/Ames-Lakers/p/11105697.html