Skip to main content

Networking in Python

The Hacker’s Digital Wires

Imagine you’re a hacker connecting two machines across cyberspace. You need a way to send messages, commands, data, or signals through invisible wires. That’s what networking in Python is about: using sockets to establish communication channels and protocols to define how data flows.

Networking transforms your scripts into connected systems, enabling chat apps, servers, APIs, and distributed hacks.


Why Networking Matters

  • Socket: An endpoint for sending/receiving data across a network.
  • Protocols: Rules for communication (TCP for reliable streams, UDP for fast datagrams).
  • Client‑Server Model: One side listens (server), the other connects (client).
  • Port Numbers: Identify specific services on a machine.
  • Hacker’s Advantage: Networking lets you build systems that talk to each other - remote control, monitoring, or collaboration.

TCP Socket Example

# Server
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("127.0.0.1", 9999))
server.listen()

print("Server listening...")
conn, addr = server.accept()
print(f"Connected by {addr}")

data = conn.recv(1024).decode()
print("Received:", data)
conn.send("Message received".encode())
conn.close()
# Client
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 9999))
client.send("Hello, Server!".encode())
response = client.recv(1024).decode()
print("Server says:", response)
client.close()
  • Why? TCP ensures reliable, ordered delivery of data for chat or file transfer.

UDP Socket Example

# Server
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(("127.0.0.1", 9999))

print("UDP server listening...")
data, addr = server.recvfrom(1024)
print("Received:", data.decode(), "from", addr)
server.sendto("Ack".encode(), addr)
# Client
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto("Hello via UDP".encode(), ("127.0.0.1", 9999))
response, _ = client.recvfrom(1024)
print("Server says:", response.decode())
  • Why? UDP is faster but doesn’t guarantee delivery which is ideal for streaming or gaming.

Protocols in Practice

  • TCP (Transmission Control Protocol): Reliable, ordered, connection‑oriented.
  • UDP (User Datagram Protocol): Fast, lightweight, connectionless.
  • HTTP/HTTPS: Built on TCP, used for web communication.
  • FTP/SMTP: Specialized protocols for file transfer and email.

Real‑World Example

# Server
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("127.0.0.1", 8888))
server.listen()
print("Chat server running...")

conn, addr = server.accept()
print("Connected:", addr)

while True:
    msg = conn.recv(1024).decode()
    if msg.lower() == "exit":
        break
    print("Client:", msg)
    conn.send(f"Echo: {msg}".encode())

conn.close()
# Client
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 8888))

while True:
    msg = input("You: ")
    client.send(msg.encode())
    if msg.lower() == "exit":
        break
    response = client.recv(1024).decode()
    print("Server:", response)

client.close()
  • Why? Demonstrates client‑server communication with sockets, forming the basis of chat apps.

The Hacker’s Notebook

  • Sockets are endpoints for network communication. TCP provides reliable, ordered delivery; UDP offers speed with less reliability.
  • Protocols define communication rules - HTTP, FTP, SMTP build on sockets. Client‑server models enable two‑way communication across machines.

Hacker’s Mindset: treat networking as your digital wires. With sockets and protocols, you connect systems, build communication tools, and orchestrate distributed hacks.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Jan 3, 2026