30 lines
845 B
Python
30 lines
845 B
Python
|
from socket import *
|
||
|
class udp(object):
|
||
|
"""wifi udp to esp32"""
|
||
|
def __init__(self,HOST = '192.168.4.1',PORT = 2333):
|
||
|
|
||
|
self.HOST = HOST
|
||
|
self.PORT =PORT
|
||
|
self.BUFSIZ = 1024
|
||
|
self.ADDRESS = (self.HOST, self.PORT)
|
||
|
|
||
|
self.udpClientSocket = socket(AF_INET, SOCK_DGRAM)
|
||
|
self.udpClientSocket.bind(("192.168.4.2",2333))
|
||
|
|
||
|
def send_message(self,data):
|
||
|
if not data:
|
||
|
return 0
|
||
|
self.udpClientSocket.sendto(data.encode('utf-8'),self.ADDRESS)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
udp = udp()
|
||
|
while True:
|
||
|
data = input('>')
|
||
|
udp.send_message(data)
|
||
|
# 接收数据
|
||
|
data, ADDR = udp.udpClientSocket.recvfrom(udp.BUFSIZ)
|
||
|
if not data:
|
||
|
break
|
||
|
print("服务器端响应:", data.decode('utf-8'))
|
||
|
|
||
|
udp.udpClientSocket.close()
|