网络编程之 tcp/udp 协议基础代码, 时间同步机制, pycharm着色 (二)
今日内容:
1, tcp协议的聊天
服务器(server):
# 服务器:
import socket sk = socket.socket() # 创建 套接字 sk.bind(('127.0.0.1', 9090)) # 绑定 端口 sk.listen()# 监听 flag = True while flag: con, addr = sk.accept() while 1: msg = con.recv(1024).decode('utf-8') print(f'客户端:{addr}发送了:{msg}') if msg == 'q': break anime = input('>>>') con.send(anime.encode('utf-8')) if anime.lower() == 'q': flag = False break con.close() sk.close()
客户端(client):
# 客户端 import socket sk = socket.socket() sk.connect(('127.0.0.1', 9090)) while 1: user = input('>>>') if user.lower() == 'q': break sk.send(user.encode('utf-8')) msg = sk.recv(1024).decode('utf-8') print(msg) if msg == 'q': break sk.close()
阻塞: 让程序停下 堵住.
accept() 等待连接
recv() 等待接收客户端打印的消息
input() 等待用户输入.
socket.socket(family = socket.AF_INET, type = SOCK_STREAM )
参数1: family: 套接字类型
family = socket.AF_INET 采用网络形式通信的套接字
family = socket.AF_UNIX 采用文件形式通信的套接字
参数2 type : 通信协议类型 tcp协议, udp 协议
tcp: SOCK_STREAM
udp: SOCK_DGRAM
2, udp协议的聊天
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.recvfrom
服务器(server):
# 服务器 from socket import socket, SOCK_DGRAM sk = socket(type=SOCK_DGRAM) sk.bind(('127.0.0.1', 9090)) while 1: msg_r, addr = sk.recvfrom(1024) msg_r = msg_r.decode('utf-8') print(f'客户端:{addr},发来消息:{msg_r}') if msg_r.lower() == 'q': continue msg_s = input('>>>') sk.sendto(msg_s.encode('utf-8'), addr) if msg_s.lower() == 'q': break sk.close()
客户端(client):
# 客户端 from socket import socket, SOCK_DGRAM sk = socket(type=SOCK_DGRAM) while 1: msg_s = input('>>>') sk.sendto(msg_s.encode('utf-8'), ('127.0.0.1', 9090)) if msg_s.lower() == 'q': break msg_r, addr = sk.recvfrom(1024) msg_r = msg_r.decode('utf-8') print(f'客户端:{addr},发来消息:{msg_r}') if msg_r.lower() == 'q': break sk.close()
3, 基于udp协议的时间同步机制.
服务器(server):
# 基于 udp协议 的服务器 import socket import time sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1',9090)) while 1: tm_format,addr = sk.recvfrom(1024) tm_format = tm_format.decode('utf-8')# %Y-%m\%d %H:%M:%S local_tm = time.strftime(tm_format)# 获取到了对应格式的当前时间 sk.sendto(local_tm.encode('utf-8'),addr)# 返回给客户端 sk.close()
客户端(client):
# 基于 udp 协议 的 客户端 import socket import time sk = socket.socket(type=socket.SOCK_DGRAM) tm_format = input('>>>') while 1: sk.sendto(tm_format.encode('utf-8'),('127.0.0.1',9090)) local_tm,addr = sk.recvfrom(1024) print(local_tm.decode('utf-8')) time.sleep(2)
4, 在pycharm上实现着色
'\033[0;32;40m欢迎使用学生选课系统\033[0m' 这是一个字符串, 着色其实就是 用 \033[X1;X2;X3m 和 \033[0m 包围起来的字符串 会改变颜色. X2 是背景色, X3 是字体颜色.
详情内容请见:https://www.cnblogs.com/Eva-J/p/8330517.html
服务器代码:
import socket sk = socket.socket(type=socket.SOCK_DGRAM)# udp协议 sk.bind(('127.0.0.1',9090)) dic = {'alex':'\033[0;33;42m','太白':'\033[0;35;40m'} while 1: msg_r,addr = sk.recvfrom(1024)# 接收来自哪里的消息 msg_r = msg_r.decode('utf-8')# alex : 我要退学 # 对于msg_r,通过':'分割,获取下标为0的,也就是name,再去掉name的左右两边的空格 name = msg_r.split(':')[0].strip() color = dic.get(name,'')# 获取字典中 name所对应的 颜色值 print('%s%s \033[0m'%(color,msg_r)) if msg_r == 'q':# 如果当前客户端想要断开连接 continue # 服务器端不应该继续通话了,应该等待接收另一个客户端的连接,返回到recvfrom msg_s = input('>>>') sk.sendto(msg_s.encode('utf-8'), addr) if msg_s == 'q': break sk.close()
客户端代码:
import socket sk = socket.socket(type=socket.SOCK_DGRAM) name = input('请输入您的名字:>>>') while 1: msg_s = input('>>>') msg_s = name + " : "+msg_s sk.sendto(msg_s.encode('utf-8'),('127.0.0.1',9090))# 发给谁一条消息 if msg_s is 'q': break msg_r,addr = sk.recvfrom(1024) msg_r = msg_r.decode('utf-8') print(msg_r) if msg_r == 'q': break sk.close()