Python network communication IPv6 over TCP socket

Petr Faltus development

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

net_tcp6_receiver.py

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

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

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

import socket

RECEIVER_ADDRESS = "::1"
RECEIVER_PORT = 10000
RECEIVED_MESSAGES_MAX = 10
BUFFER_SIZE = 4096

print("TCP IPv6 stream socket receiver")

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

receiver = (RECEIVER_ADDRESS, RECEIVER_PORT);

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

receivingSocket.listen(RECEIVED_MESSAGES_MAX)
print("- socket is listening for max " + str(RECEIVED_MESSAGES_MAX) + " messages")

stop = False
while (stop == False):
    (msgsock, peer) = receivingSocket.accept()
    print("- socket accepted request")

    (peer_address, peer_port, peer_object3, peer_object4) = peer
    (local_address, local_port, local_object3, local_object4) = msgsock.getsockname()
    print("- peer connect from [" + peer_address + "]:" + str(peer_port) + " on [" + local_address + "]:" + str(local_port))

    buffer = msgsock.recv(BUFFER_SIZE)
    received_length = len(buffer)
    msg = buffer.decode()
    print("- message " + str(received_length) + "B received")
    print("|" + msg + "|")

    if msg == "stop":
        stop = True

    msgsock.close()
    print("- socket closed request")

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

net_tcp6_sender.py

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

import socket
import sys
from datetime import datetime

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

print("TCP IPv6 stream socket sender")

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

receiver_address = (RECEIVER_ADDRESS, RECEIVER_PORT)

sendingSocket.connect(receiver_address)
print("- connected to [" + RECEIVER_ADDRESS + "]:" + str(RECEIVER_PORT))

(local_address, local_port, local_object3, local_object4) = sendingSocket.getsockname()
print("- for [" + RECEIVER_ADDRESS + "]:" + str(RECEIVER_PORT) + " bound on [" + local_address + "]:" + str(local_port))

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)

msg_length = len(msg)
sendingSocket.send(buffer)
print("- message " + str(msg_length) + "B sent")

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 44.221.73.157 (ec2-44-221-73-157.compute-1.amazonaws.com)