Python network communication IPv6 over UDP socket

Petr Faltus development

Example source code in Python how to communicate in IPv6 over network UDP socket. The sender and the receiver.

net_udp6_receiver.py

GitHub repository: network-socket-communication-source-codes

git clone https://github.com/petrfaltus/network-socket-communication-source-codes.git

Directories to net_udp6_receiver.py in the repository: python/src

import socket

RECEIVER_ADDRESS = "::0"
RECEIVER_PORT = 10000
BUFFER_SIZE = 4096

print("UDP IPv6 datagram socket receiver")

receivingSocket = socket.socket(family=socket.AF_INET6, type=socket.SOCK_DGRAM)
print("- socket created")

receiver = (RECEIVER_ADDRESS, RECEIVER_PORT);

receivingSocket.bind(receiver)
print("- socket bound on [" + RECEIVER_ADDRESS + "]:" + str(RECEIVER_PORT))

stop = False
while (stop == False):
    (buffer, peer) = receivingSocket.recvfrom(BUFFER_SIZE)
    received_length = len(buffer)
    msg = buffer.decode()
    (peer_address, peer_port, peer_object3, peer_object4) = peer
    print("- message " + str(received_length) + "B received from [" + peer_address + "]:" + str(peer_port) + " on port " + str(RECEIVER_PORT))
    print("|" + msg + "|")

    if msg == "stop":
        stop = True

receivingSocket.close()
print("- socket closed")

net_udp6_sender.py

Directories to net_udp6_sender.py in the repository: python/src

import socket
import sys
from datetime import datetime

RECEIVER_ADDRESS = "::1";
RECEIVER_PORT = 10000;

print("UDP IPv6 datagram socket sender")

sendingSocket = socket.socket(family=socket.AF_INET6, type=socket.SOCK_DGRAM)
print("- socket created")

if len(sys.argv) > 1:
    msg = sys.argv[1] # message is the first parameter, for example "stop" to stop the receiver
else:
    now = datetime.now()
    msg = "This is Python message sent at " + now.strftime("%d.%m.%Y") + " in " + now.strftime("%H:%M:%S")
print("|" + msg + "|")

buffer = str.encode(msg)
receiver_address = (RECEIVER_ADDRESS, RECEIVER_PORT)

msg_length = len(msg)
sendingSocket.sendto(buffer, receiver_address)

(local_address, local_port, local_object3, local_object4) = sendingSocket.getsockname()
print("- message " + str(msg_length) + "B sent to [" + RECEIVER_ADDRESS + "]:" + str(RECEIVER_PORT) + " on port " + str(local_port))

sendingSocket.close();
print("- socket closed")

Development tools

Developer ASCII table
Characters and HTML entities in the UTF-8 table
 
Predefined web CSS colors
CSS px to rem converter

 

  

🤝 Your IP address is 3.147.47.167 (ec2-3-147-47-167.us-east-2.compute.amazonaws.com)