python之路----TCP与UDP
TCP
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket #tcp协议 sk = socket.socket() # 买手机 创建一个socket对象 sk.bind(('127.0.0.1',8080)) # 给server端绑定一个ip和端口 sk.listen() # py3.4 while True: conn,addr = sk.accept() # 获取到一个客户端的连接, 已经完成了三次握手建立了一个连接 # 阻塞 while True: msg = conn.recv(1024).decode('utf-8') # 阻塞,直到收到一个客户端发来的消息 print(msg) if msg == 'bye':break info = input('>>>') if info == 'bye': conn.send(b'bye') break conn.send(info.encode('utf-8')) # 发消息 conn.close() # 关闭连接 sk.close() # 关闭socket对象,如果不关闭,还能继续接收 # server client1 建立了长连接 # client2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: msg = input('>>>') if msg == 'bye': sk.send(b'bye') break sk.send(msg.encode('utf-8')) ret = sk.recv(1024).decode('utf-8') if ret == 'bye':break print(ret) sk.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: msg = input('client2:>>>') if msg == 'bye': sk.send(b'bye') break sk.send(('client2 :'+msg).encode('utf-8')) ret = sk.recv(1024).decode('utf-8') if ret == 'bye':break print(ret) sk.close()
UDP
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket(type=socket.SOCK_DGRAM) #DGRAM datagram sk.bind(('127.0.0.1',8080)) #只有服务端有的 msg,addr = sk.recvfrom(1024) print(msg.decode('utf-8')) sk.sendto(b'bye',addr) sk.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket(type=socket.SOCK_DGRAM) ip_port = ('127.0.0.1',8080) sk.sendto(b'hello',ip_port) ret,addr = sk.recvfrom(1024) print(ret.decode('utf-8')) sk.close()
UDP协议实现qq聊天
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1',8080)) while True: msg,addr = sk.recvfrom(1024) print(addr) print(msg.decode('utf-8')) info = input('>>>').encode('utf-8') sk.sendto(info,addr) sk.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket(type=socket.SOCK_DGRAM) ip_port = ('127.0.0.1',8080) while True: info = input('tiger :') info = ('\033[34m来自tiger的消息 :%s\033[0m'%info).encode('utf-8') sk.sendto(info,ip_port) msg,addr = sk.recvfrom(1024) print(msg.decode('utf-8')) sk.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket(type=socket.SOCK_DGRAM) ip_port = ('127.0.0.1',8080) while True: info = input('二哥 :') info = ('\033[32m来自二哥的消息 :%s\033[0m'%info).encode('utf-8') sk.sendto(info,ip_port) msg,addr = sk.recvfrom(1024) print(msg.decode('utf-8')) sk.close()
UDP实现系统时间同步
# 提供服务
# 接收信息 时间的格式
# 将我的时间转换成 接受到的格式
# 发回给客户端
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1',8090)) while True: strf,addr = sk.recvfrom(1024) strf = strf.decode('utf-8') res = time.strftime(strf).encode('utf-8') sk.sendto(res,addr) sk.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket(type=socket.SOCK_DGRAM) addr = ('127.0.0.1',8090) info = input('>>>').encode('utf-8') sk.sendto(info,addr) ret,addr = sk.recvfrom(1024) print(ret.decode('utf-8')) sk.close()