udp协议的编码及时间同步机制,颜色字体编码
服务器::
import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# sk.bind(('192.168.19.30',9090))
# while True:
# msg_r,addr=sk.recvfrom(1024)
# print(addr,msg_r.decode('utf-8'))
# s=input('>>>>')
# sk.sendto(s.encode('utf-8'),addr)
# sk.close()
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# sk.bin(('127.0.0.1',9090))
# while 1:
# ret,addr=sk.recvfrom(1024)#接受来自哪里的消息
# ret=ret.decode('utf-8')
# print(ret)
# if ret=='q':
# continue
# s=input('>>>')
# sk.sendto(a.encode('utf-8'),addr)
# if s=='q':
# break
# sk.close()
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# sk.bind(('127.0.0.1',9090))
# while 1:
# ret,addr=sk.recvfrom(1024)
# ret=ret.decode('utf-8')
# print(ret)
# if ret=='q':
# continue
# a=input('>>>')
# sk.sento(a.encode('utf-8'),addr)
# if a=='q':
# break
# sk.close()
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# sk.bind(('127.0.0.1',9090))
# while 1:
# ret,addr=sk.recvfrom(1024)
# ret=ret.decode('utf-8')
# print(ret)
# if ret=='q':
# continue
# s=input('>>>')
# sk.sendto(s.encode('utf-8'),addr)
# if s =='q':
# break
# sk.close()
对应服务器的客户端:::
import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# while 1:
# a=input('>>>>>')
# if a=='q':
# break
# sk.sendto(a.encode(),('127.0.0.1',9090))
# msg_r,addr=sk.recvfrom(1024)
# print(msg_r.decode('utf-8'))
# sk.close()
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# while 1:
# a=input('>>>')
# sk.sendto(a.encode('utf-8'),('127.0.0.1',9090))
# if a=='q':
# break
# ret,addr=sk.recvfrom(1024)
# ret=ret.decode('utf-8')
# print(ret)
# if ret=='q':
# break
# sk.close()
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# while 1:
# a=input(">>>")
# sk.sendto(a.encode('utf-8'),('127.0.0.1',9090))#发送给谁一条消息
# if a=='q':
# break
# ret,addr=sk.recvfrom(1024)
# ret=ret.decode('utf-8')
# print(ret)
# if ret =='q':
# break
# sk.close()
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# while 1:
# a=input('>>>')
# sk.sendto(a.encode('utf-8'),('127.0.0.1'))
# if a =='q':
# break
# ret,addr=sk.recvfrom(1024)
# ret=ret.decode('utf-8')
# print(ret)
# if ret=='q':
# break
# sk.close()
带颜色字体,,,客户端编码
带颜色的udp协议编码
# # import socket
# # sk=socket.socket(type=socket.SOCK_DGRAM)
# # name=input('输入你的名字')
# # while 1:
# # a=input('>>>')
# # a=name+':'+a
# # sk.sendto(a.encode('utf-8'),('127.0.0.1',9090))
# # if a =='q':
# # break
# # ret,addr=sk.recvfrom(1024)
# # ret=ret.decode('utf-8')
# # print(ret)
# # if ret=='q':
# # break
# # sk.close()
#
# # import socket
# # sk=socket.socket(type=socket.SOCK_DGRAM)
# # name=input('请输入你的名字')
# # while 1:
# # a=input('>>>')
# # a=name+':'+a
# # sk.sendto(a.encode('utf-8'),('127.0.0.1',9090))
# # if a=='q':
# # break
# # ret,addr=sk.recvfrom(1024)
# # ret=ret.decode('utf-8')
# # print(ret)
# # if ret=='q':
# # break
# # sk.close()
带颜色字体,服务器编码
带颜色的服务器代码:
# import socket
# sk=socket.socket(type=socket.SOCK_DGRAM)
# sk.bind(('127.0.0.1',9090))
# dic={'alex':'\033[0;33;42m','wuse':'\033[0;35;40m'}
# while 1:
# ret,addr=sk.recvfrom(1024)#接收消息
# ret=ret.decode('utf-8') #转换格式
# #对于ret通过分割获取name,再去掉name左右两边的空格.
# name=ret.split(':')[0].strip()
# color=dic.get(name,'')#获取字典中name所对应的颜色值
# print('%s%s \033[0m' %(color ,ret))
# if ret=='q':
# continue
# a=input('>>>')
# sk.sendto(a.encode('utf-8'),addr)
# if a=='q':
# break
# sk.close()
# import socket
# # sk=socket.socket(type=socket.SOCK_DGRAM)
# # sk.bind(('127.0.0.1',9090))
# # dic={'alex':'\033[0;31;41m','wuse':'\033[0;32;39m'}
# # while 1:
# # ret,addr=sk.recvfrom(1024)
# # ret=ret.decode('utf-8')
# # name=ret.split(':')[0].strip()
# # color=dic.get(name,'')
# # print('%s%s \033[0m'%(color,ret))
# # if ret=='q':
# # continue
# # a=input('>>')
# # sk.sendto(a.encode('utf-8'),addr)
# # if a=='q':
# # break
# # sk.close()
时间同步机制服务器编码
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')
# local_tm=time.strftime(tm_format)#获取对应格式的当前时间
# sk.sendto(local_tm.encode('utf-8'))
# sk.close()
# import socket
# import time
# sk=socket.socket(type=socket.SOCK_DGRAM)
# sk.bind(('127.0.0.1',9090))
# while 1:
# ret,addr=sk.recvfrom(1024)
# print(ret.decode('utf-8'))
# local_tm=time.retftime(ret)
# sk.sendto(local_tm.encode('utf-8'),addr)
# sk.close()
时间同步机制客户端编码
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)
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))
ret,addr=sk.recvfrom(1024)
ret=ret.decode('utf-8')
print(ret)
time.sleep(2)