1 ,建 tcp 对象 :
sk = socket.socket()
2 ,建 udp 对象 :
udp = socket.socket(type=socket.SOCK_DGRAM)
3 ,服务端绑定地址 :
sk.bind(('127.0.0.1', 8888))
4 ,tcp 收发消息 :
sk.send(content)
content = conn.recv(buffer)
5 ,udp 收发消息 :
udp.sendto(msg.encode("utf-8"),("127.0.0.1",8080))
msg,addr = udp.recvfrom(1024)
6 ,sk.sendall(“xxx”) :
尝试发送string的所有数据, 成功则返回None, 失败则抛出异常。
7 ,zk.send() :
返回值式发送的字节数量, 这个数量值可能小于要发送的 string 的字节数,也就是说可能无法发送 string 中所有的数据。如果有错误,则会抛出异常。
8 ,阻塞 :
- 默认 : 阻塞
- 设置阻塞 : sk.setblocking(True)
- 设置非阻塞 : sk.setblocking(False)