当在windows和Mac上,练习对应的代码时
非常明显的错误就在于 传输类型
TypeError: a bytes-like object is required, not 'str' TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str self.wfile.write(b'[%s] %s' % (bytes(ctime(), 'utf-8'), bytes(self.rfile.readline(), 'utf-8'))) TypeError: encoding without a string argument 主要都是因为,通信的接收和发送都是byte的类型。
所以需要使用
bytes() 函数
以下是可以正常运行的代码
from socketserver import (TCPServer as TCP, StreamRequestHandler as SRH) from time import ctime HOST = "" PORT = 21467 BUFSIZ = 1024 ADDR = (HOST, PORT) class MyRequestHandler(SRH): def handle(self): print('connect. ...