day30-python之socket
1.iter补充
# l=['a','b','c','d'] # # def test(): # return l.pop() # # x=iter(test,'b') # print(x.__next__()) # print(x.__next__()) # print(x.__next__()) from functools import partial def add(x,y): return x+y func = partial(add,1) print(func(1)) print(func(2)) # from functools import partial # def add(x,y): # return x+y # # print(add(1,2)) # # func=partial(add,1) #偏函数 # print(func(1)) # print(func(2)) # recv_size = 0 # recv_msg = b'' # while recv_size < length: # recv_msg += tcp_client.recv(buffer_size) # recv_size = len(recv_msg) #1024 # ''.join(iter(partial(tcp_client.recv,1024),b''))
2.ntp_client和ntp_server
# from socket import * # import time # ip_port=('127.0.0.1',8080) # buffer_size=1024 # # udp_server=socket(AF_INET,SOCK_DGRAM) #数据报 # udp_server.bind(ip_port) from socket import * import time ip_port = ('127.0.0.1',8080) buffer_size = 1024 udp_server = socket(AF_INET,SOCK_DGRAM) udp_server.bind(ip_port) while True: data,addr =udp_server.recvfrom(buffer_size) print(data) if not data: fmt = '%Y-%m-%d %X' else: fmt = data.decode('utf-8') back_time = time.strftime(fmt) udp_server.sendto(back_time.encode('utf-8'),addr) # while True: # data,addr=udp_server.recvfrom(buffer_size) # print(data) # # if not data: # fmt='%Y-%m-%d %X' # else: # fmt=data.decode('utf-8') # back_time=time.strftime(fmt) # # udp_server.sendto(back_time.encode('utf-8'),addr)
# from socket import * # ip_port=('127.0.0.1',8080) # buffer_size=1024 # # udp_client=socket(AF_INET,SOCK_DGRAM) #数据报 # # while True: # msg=input('>>: ').strip() # udp_client.sendto(msg.encode('utf-8'),ip_port) # # data,addr=udp_client.recvfrom(buffer_size) # print('ntp服务器的标准时间是',data.decode('utf-8')) from socket import * ip_port = ('127.0.0.1',8080) buffer_size = 1024 udp_client = socket(AF_INET,SOCK_DGRAM) while True: msg = input('>>: ').strip() udp_client.sendto(msg.encode('utf-8'),ip_port) data,addr=udp_client.recvfrom(buffer_size) print('ntp服务器的标准时间是',data.decode('utf-8'))
3.time复习
# import time import time # print(time.time()) # print(time.localtime(time.time())) # print(time.gmtime(time.time()+28800)) # print(time.mktime(time.localtime())) # print(time.strftime('%Y-%m-%d %X',time.localtime())) # print(time.strftime('%F %X',time.localtime())) print(time.ctime()) # print(time.time()) #用于计算 # print(time.localtime(time.time())) # print(time.gmtime(time.time()+28800)) # print(time.mktime(time.localtime())) #格式化-->时间戳 # print(time.strftime('%Y-%m-%d %X',time.localtime())) #格式化--》字符串时间 # print(time.strftime('%F %X',time.localtime())) # print(time.strptime('2017-01-03 09:37:06','%Y-%m-%d %X')) #字符串--》格式化 # print(time.ctime())
4.udp的客户端与服务端通信
from socket import * ip_port=('127.0.0.1',8081) buffer_size=1024 udp_client=socket(AF_INET,SOCK_DGRAM) #数据报 while True: msg=input('>>: ').strip() udp_client.sendto(msg.encode('utf-8'),ip_port) data,addr=udp_client.recvfrom(buffer_size) # print(data.decode('utf-8')) print(data)
# from socket import * # ip_port=('127.0.0.1',8080) # buffer_size=1024 # # udp_server=socket(AF_INET,SOCK_DGRAM) #数据报 # udp_server.bind(ip_port) # from socket import * ip_port = ('127.0.0.1',8081) buffer_size = 1024 udp_server = socket(AF_INET,SOCK_DGRAM) udp_server.bind(ip_port) while True: data,addr = udp_server.recvfrom(buffer_size) print(data) udp_server.sendto(data.upper(),addr) # while True: # data,addr=udp_server.recvfrom(buffer_size) # print(data) # # udp_server.sendto(data.upper(),addr)
5.解决粘包
# from socket import * # ip_port=('127.0.0.1',8080) # back_log=5 # buffer_size=1024 # # tcp_server=socket(AF_INET,SOCK_STREAM) # tcp_server.bind(ip_port) # tcp_server.listen(back_log) # from socket import * ip_port = ('127.0.0.1',8082) back_log = 5 buffer_size = 1024 tcp_server = socket(AF_INET,SOCK_STREAM) tcp_server.bind(ip_port) tcp_server.listen(back_log) conn,addr = tcp_server.accept() data1 = conn.recv(5) print('第一次数据',data1) data2 = conn.recv(5) print('第二次数据',data2) data3 = conn.recv(5) print('第三次数据',data3) # conn,addr=tcp_server.accept() # # data1=conn.recv(5) # print('第一次数据',data1) # # data2=conn.recv(5) # print('第2次数据',data2) # # data3=conn.recv(5) # print('第3次数据',data3)
# from socket import * # import time # ip_port=('127.0.0.1',8080) # back_log=5 # buffer_size=1024 # # tcp_client=socket(AF_INET,SOCK_STREAM) # tcp_client.connect(ip_port) # from socket import * import time ip_port = ('127.0.0.1',8082) back_log = 5 buffer_size = 1024 tcp_client = socket(AF_INET,SOCK_STREAM) tcp_client.connect(ip_port) tcp_client.send('hello'.encode('utf-8')) tcp_client.send('world'.encode('utf=8')) tcp_client.send('egon'.encode('utf-8')) # tcp_client.send('hello'.encode('utf-8')) # tcp_client.send('world'.encode('utf-8')) # tcp_client.send('egon'.encode('utf-8')) # # # # time.sleep(1000)