python socket udp

from socket import *
from time import ctime

host = '' #监听所有的ip
port = 12348 #接口必须一致
bufsize = 1024
addr = (host,port) 

udpServer = socket(AF_INET,SOCK_DGRAM)
udpServer.bind(addr) #开始监听

while True:
    print('Waiting for connection...')
    data,addr = udpServer.recvfrom(bufsize)  #接收数据和返回地址
    #处理数据
    data  = data.decode(encoding='utf-8').upper()
    data = "at %s :%s"%(ctime(),data)
    print('recv', data)
    #udpServer.sendto(data.encode(encoding='utf-8'),addr)
    #发送数据
    #print('...recevied from and return to :',addr)

udpServer.close()
import socket
import time 

host = '127.0.0.1'    
#两个段口必须一致
port = 12348
addr = (host, port)
byte = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
    data = time.strftime("100$121$231$%H:%M:%S:00", time.localtime())
    #data = time.strftime("%H:%M:%S", time.localtime()) 
    text = data.encode('utf-8')
    sock.sendto(text, addr)
sock.close()

猜你喜欢

转载自blog.csdn.net/chao56789/article/details/81162288
今日推荐