Skip to content
Python

Socket Programming

TCP Socket server and client.

By EZ4Code Team
socketnetwork

Code

import socket

# Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 8080))
server.listen(5)
conn, addr = server.accept()
with conn:
    while True:
        data = conn.recv(1024)
        if not data:
            break
        conn.sendall(data)

# Client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("localhost", 8080))
client.sendall(b"hello")
print(client.recv(1024))

Explanation

The socket module provides low-level network communication capabilities.

More Python Snippets