python创建TCPSever,python2和python3基本相同,除了以下区别:
- python2引用SocketServer库,python3引用socketserver库
- 发送字符串,python2可直接调用self.wfile.write(content),python3需调用self.wfile.write(content.encode()),否则会出现需要传入参数错误:“TypeError: a bytes-like object is required, not ‘str’”
创建tcp服务器的代码如下:
import struct
import time
from SocketServer import (TCPServer as TCP, StreamRequestHandler as SRH) # python2
from socketserver import