# import socket
# import time
#
# sk = socket.socket(type = socket.SOCK_DGRAM)
#
# print(
# '''
# %Y 年
# %m 月
# %d 日
# '''
# )
# time_fo = input('请输入格式')
# while 1:
# sk.sendto(time_fo.encode('utf-8'),('127.0.0.1',8091))
#
# time_now , addr = sk.recvfrom(1024)
# print(time_now.decode('utf-8'))
# time.sleep(3)
# sk.close()
#客户端,每隔一定时间段去服务器获取一下准确时间。
# import socket
# import time
#
# sk = socket.socket(type=socket.SOCK_DGRAM)
#
# print(
# '''
# %Y:年
# %m:月
# %d:日
# %H:时
# %M:分
# %S:秒
# '''
# )
# form = input('请输入一个格式').strip()
# while 1:
# sk.sendto(form.encode('utf-8'),('127.0.0.1',8000))
# time_now ,addr = sk.recvfrom(1024)
# print(time_now.decode('utf-8'))
# time.sleep(2)
# sk.close()
udp获取时间,客户端
# import socket
# import time
#
# sk = socket.socket(type= socket.SOCK_DGRAM)
#
# sk.bind(('127.0.0.1',8091))
#
# while 1:
# time_form, addr = sk.recvfrom(1024)
# time_now = time.strftime(time_form.decode('utf-8'))
#
# sk.sendto(time_now.encode('utf-8'),addr)
#
# sk.close()
#服务器端随时等待给客户端发送准确时间。
# import socket
# import time
#
# sk = socket.socket(type=socket.SOCK_DGRAM)
#
# sk.bind(('127.0.0.1',8000))
#
# while 1:
# time_format , addr = sk.recvfrom(1024)
# msg_s = time.strftime(time_format.decode('utf-8'))
# sk.sendto(msg_s.encode('utf-8'),addr)
# sk.close()
udp获取时间,服务端
# import socket
#
# sk = socket.socket(type= socket.SOCK_DGRAM)
#
# name = input('请输入您的名字').strip()
#
# while 1:
# msg_s = input('>>>')
# sk.sendto((name+':'+ msg_s).encode('utf-8'),('127.0.0.1',8090))
# if msg_s == 'q':
# break
#
# msg_r,addr = sk.recvfrom(1024) #用两个变量来接收,一个是接收的内容,另一个是元组形式的ip地址和端口(addr)
# if msg_r.decode('utf-8') == 'q':
# break
# print(msg_r.decode('utf-8'))
#
# sk.close()
# import socket
#
# sk = socket.socket(type=socket.SOCK_DGRAM)
#
# name = input('请输入您的名字').strip()
#
# while 1:
# msg_s = input('>>>')
# if msg_s == 'q':
# break
# sk.sendto((name+':'+msg_s).encode('utf-8'),('127.0.0.1',8090))
#
# msg_r , addr= sk.recvfrom(1024)#一定要用两个变量接收,得到的是两个返回值。
# print(msg_r.decode('utf-8'))
# if msg_r.decode('utf-8') == 'q':
# break
# sk.close()
udp协议客户端
# import socket
#
# sk = socket.socket(type= socket.SOCK_DGRAM)
#
# sk.bind(('127.0.0.1',8090))
#
# ls = { 'alex' : ' 33[5;32m','egon': ' 33[5;41m'}
#
# while 1:
# msg_r , addr = sk.recvfrom(1024)
# if msg_r.decode('utf-8') == 'q':
# print('%s的客户端发来消息: %s并退出'% (addr,msg_r.decode('utf-8')))
# continue
# # print(addr)
# # print('%s的客户端退出了' % (addr))
# name = msg_r.decode('utf-8').split(':')[0]
# color = ls.get(name,'')
# print('%s %s 33[0m'%(color,msg_r.decode('utf-8')))
#
# msg_s = input('>>>')
# sk.sendto(msg_s.encode('utf-8'),addr)
# if msg_s == 'q':
# break
#
# sk.close()
#同一台电脑上的两个文件通信:通过文件
#tcp协议:不允许同时几个客户端一起连接服务器。
#udp协议:允许同时几个客户端一起连接服务器。
#udp: 不面向连接。没有listen和accept。谁连接过来addr就变成谁的,进而给当前的addr发送消息。
# import socket
#
# sk = socket.socket(type=socket.SOCK_DGRAM)#udp协议要改变type默认参数
#
# sk.bind(('127.0.0.1',8090)) #跟tcp协议一样,需要绑定ip和端口。
#
# dic = {'alex': ' 33[1;43;30m',
# 'liujunjie':' 33[1;32m'
# }
#
# while 1:
# msg_r , addr = sk.recvfrom(1024) #udp中接收用recvfrom,得到两个返回值。接收的的内容和地址。
# name = msg_r.decode('utf-8').split(':')[0]
# color = dic.get(name,'')
# print('%s %s 33[0m'%(color,msg_r.decode('utf-8')))
#
# msg_s = input('>>>')
# sk.sendto(msg_s.encode('utf-8'),addr)#udp中发送用sendto,两个参数:内容和客户端地址。
# sk.close()