OVMS3-idf/examples/protocols/sockets/scripts/udpclient.py

50 lines
1.4 KiB
Python
Raw Normal View History

2018-10-02 14:33:16 +00:00
# This example code is in the Public Domain (or CC0 licensed, at your option.)
# Unless required by applicable law or agreed to in writing, this
# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied.
# -*- coding: utf-8 -*-
from builtins import input
import socket
2018-12-04 07:32:48 +00:00
import sys
2018-10-02 14:33:16 +00:00
2018-12-04 07:32:48 +00:00
# ----------- Config ----------
2018-10-02 14:33:16 +00:00
PORT = 3333
IP_VERSION = 'IPv6'
2018-10-02 14:33:16 +00:00
IPV4 = '192.168.0.167'
IPV6 = 'fe80:0000:0000:0000:260a:c4ff:fe11:a1e0%wlp1s0'
2018-10-02 14:33:16 +00:00
# -------------------------------
if IP_VERSION == 'IPv4':
addr = (IPV4, PORT)
2018-10-02 14:33:16 +00:00
family_addr = socket.AF_INET
elif IP_VERSION == 'IPv6':
family_addr = socket.AF_INET6
for res in socket.getaddrinfo(IPV6, PORT, socket.AF_INET6,
socket.SOCK_DGRAM, socket.SOL_UDP):
af, socktype, proto, canonname, addr = res
2018-10-02 14:33:16 +00:00
else:
print('IP_VERSION must be IPv4 or IPv6')
sys.exit(1)
try:
2018-12-04 07:32:48 +00:00
sock = socket.socket(family_addr, socket.SOCK_DGRAM)
except socket.error:
2018-10-02 14:33:16 +00:00
print('Failed to create socket')
sys.exit()
2018-12-04 07:32:48 +00:00
2018-10-02 14:33:16 +00:00
while True:
msg = input('Enter message to send : ')
try:
sock.sendto(msg.encode(), addr)
2018-10-02 14:33:16 +00:00
reply, addr = sock.recvfrom(128)
2018-12-04 07:32:48 +00:00
if not reply:
break
2018-10-02 14:33:16 +00:00
print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + str(reply))
except socket.error as msg:
print('Error Code : ' + str(msg[0]) + ' Message: ' + msg[1])
2018-12-04 07:32:48 +00:00
sys.exit()