网络编程-socket报错a bytes-like object is required, not 'str'

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

今天根据书上的代码敲了一下tcp使用套接字(socket)的一些code,但是发现一直在报错。而且与书上的一模一样。代码是这样的:

  1. 客户端
from socket import *
HOST = '127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

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

while True:
    data = input('> ')
    print(data)
    if not data:
        break
    tcpCliSock.send(data)
    # 接收tcp消息
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print(data.decode('utf-8'))

tcpCliSock.close()
  1. 服务器端
from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUGSIZ = 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(BUGSIZ)
        if not data:
            break
        print(data)
        tcpCliSock.send('[%s] %s' % (bytes(ctime(),'utf-8'), data.encode()))
    tcpCliSock.close()
tcpSerSock.close()

然后就报错:a bytes-like object is required, not 'str'。
然后我就看了一下,send的源码,发现他传输的必须是byte类型,而代码中是字符串的类型。所以可以这样改一下:

  1. 客户端
from socket import *
HOST = '127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

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

while True:
    data = input('> ')
    print(data)
    if not data:
        break
    tcpCliSock.send(data.encode())
    # 接收tcp消息
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print(data.decode('utf-8'))

tcpCliSock.close()
  1. 服务器端
from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUGSIZ = 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(BUGSIZ)
        if not data:
            break
        print(data)
        # tcpCliSock.send('[%s] %s' % (bytes(ctime(),'utf-8'), data.encode()))
        tcpCliSock.send(('[%s] %s' % (ctime(), data)).encode())
    tcpCliSock.close()
tcpSerSock.close()

这样就可以正常传输了。
但是传输测试的时候发现,传过去的中文会出问题变成\xe4\xbd\xa0\xe5\xa5\xbdss'
然后重新做一下调整就OK了:

  1. 客户端
from socket import *
HOST = '127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

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

while True:
    data = input('> ')
    #print(data)
    if not data:
        break
    tcpCliSock.send(data.encode())
    # 接收tcp消息
    data = tcpCliSock.recv(BUFSIZ).decode()
    print(data)
    if not data:
        break
    print(data)

tcpCliSock.close()
  1. 服务器端
from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUGSIZ = 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(BUGSIZ)
        if not data:
            break
        print(data.decode('utf-8'))
        # tcpCliSock.send('[%s] %s' % (bytes(ctime(),'utf-8'), data.encode()))
        tcpCliSock.send(('[%s] %s' % (ctime(), data.decode())).encode())
    tcpCliSock.close()
tcpSerSock.close()

你可能感兴趣的:(网络编程-socket报错a bytes-like object is required, not 'str')