Python network communication over TCP socket

Petr Faltus development

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

net_tcp_receiver.py

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

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

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

import socket

RECEIVER_ADDRESS = "0.0.0.0"
RECEIVER_PORT = 10000
RECEIVED_MESSAGES_MAX = 10
BUFFER_SIZE = 4096

print("TCP IPv4 stream socket receiver")

receivingSocket = socket.socket(family=socket.AF_INET, 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
    (local_address, local_port) = 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_tcp_sender.py

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

import socket
import sys
from datetime import datetime

RECEIVER_ADDRESS = "127.0.0.1";
RECEIVER_PORT = 10000;

print("TCP IPv4 stream socket sender")

sendingSocket = socket.socket(family=socket.AF_INET, 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) = 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 18.218.84.153 (ec2-18-218-84-153.us-east-2.compute.amazonaws.com)