Popular Searches

Python Networking Basics

CybersecurityBeginner
Practice Now

Introduction

Welcome to the Python Networking Basics lab. In the world of cybersecurity and network administration, understanding how computers communicate over a network is an essential skill. Python provides a powerful and easy-to-use interface for network programming through its built-in libraries.

In this lab, you will learn the foundational networking capabilities of Python by writing a simple socket programming script. You will learn how to establish a raw TCP connection to a remote network service and receive the service's "banner" (a welcome message that often reveals the software version and type). This technique, known as "banner grabbing," is a fundamental step in network reconnaissance and vulnerability assessment.

This lab is important because it replaces "tool usage" with "protocol thinking". Instead of depending on a scanner to tell you what happened, you will directly create the client-side logic yourself. Once you understand sockets, many security tools become easier to reason about because you can see the underlying network conversation they automate.

By the end of this lab, you will have a working Python script capable of connecting to a target and reading its network responses.

Import Socket Library

In this step, you will begin writing your Python network script by importing the necessary library. Python includes a built-in module called socket that provides access to the BSD socket interface. This interface allows you to create network connections, send data, and receive data across different protocols like TCP and UDP.

At a high level, the socket module is Python's doorway into low-level networking. It gives your script the same basic communication primitives that many network tools are built on: open a connection, exchange bytes, then close the connection cleanly.

First, ensure you are in your default project directory. You can use the nano text editor to create a new Python file named network_basics.py.

nano ~/project/network_basics.py

Inside the nano editor, type the following line to import the socket module. This single line gives your script access to all the networking functions we will need.

import socket

At this point, you can either save the file and stay inside nano to continue with the next step, or save and exit if you prefer to pause here. In nano, press Ctrl+O to save (write out), press Enter to confirm the file name, and use Ctrl+X only when you are ready to return to the terminal.

Create a TCP Socket

Now that the socket library is imported, the next step is to create a socket object. Think of a socket as an endpoint for communication between two machines. To create a socket, you need to specify the address family (like IPv4 or IPv6) and the socket type (like TCP or UDP).

This is where abstract networking choices become concrete parameters. AF_INET answers "what kind of address will I use?" and SOCK_STREAM answers "what communication style do I want?" In this lab, that combination means IPv4 plus TCP.

If you already kept network_basics.py open in nano, continue editing the same file. Otherwise, reopen it with:

nano ~/project/network_basics.py

Add the following code below your import statement. We will use socket.AF_INET to specify that we are using IPv4 addresses, and socket.SOCK_STREAM to specify that we want a reliable, connection-oriented TCP socket.

import socket

## Create a new TCP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

The variable s now holds our socket object, which we will use in the upcoming steps to connect to a target. Save your work with Ctrl+O and Enter. You can stay in nano and continue to the next step, because you do not need to run any command yet.

Connect to a Target Port

With the socket created, you can now instruct it to connect to a specific target. A network connection requires two pieces of information: an IP address (the "where") and a port number (the "which service").

This is also a good point to remember what a port really is. It is not a physical connector. It is a logical number the operating system uses to route traffic to a particular service or process. Connecting to the right host but wrong port can reach a completely different application.

For this lab, a dummy network service has been started in the background on your local machine. It is listening on the local loopback address 127.0.0.1 at port 8080.

If you are still in nano, continue editing network_basics.py. Otherwise, reopen it:

nano ~/project/network_basics.py

Add the connection logic to your script. The connect() method takes a tuple containing the IP address as a string and the port as an integer.

import socket

## Create a new TCP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

## Define the target IP and port
target_ip = "127.0.0.1"
target_port = 8080

## Connect to the target
s.connect((target_ip, target_port))
print(f"Successfully connected to {target_ip} on port {target_port}")

After adding the connection logic, save your changes with Ctrl+O and Enter. If you want to test the script now, exit nano with Ctrl+X and run it from the terminal. If you prefer, you can stay in the editor and continue directly to the final step before running the script.

python3 ~/project/network_basics.py

You should see the following output:

Successfully connected to 127.0.0.1 on port 8080

Grab the Service Banner

The final step in our reconnaissance script is to read the data sent by the service upon connection. Many network services (like SSH, FTP, or custom applications) send a welcome message or "banner" as soon as a client connects. Capturing this banner is a common technique to identify what software and version is running on an open port.

Not every service sends a banner automatically, but when one does, it is free intelligence. A banner can reveal product names, versions, protocol details, or custom text that helps identify the service with almost no interaction.

If you kept the file open, continue editing it. Otherwise, reopen network_basics.py:

nano ~/project/network_basics.py

We will use the recv() method to read data from the socket. We need to specify a buffer size, which is the maximum amount of data to be received at once (1024 bytes is standard). Because data is transmitted as raw bytes over the network, we must decode() it into a readable string. Finally, it is good practice to close() the socket when we are done.

Update your script to look like the following:

import socket

## Create a new TCP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

## Define the target IP and port
target_ip = "127.0.0.1"
target_port = 8080

## Connect to the target
s.connect((target_ip, target_port))
print(f"Successfully connected to {target_ip} on port {target_port}")

## Receive the banner (up to 1024 bytes)
banner_bytes = s.recv(1024)

## Decode the bytes to a string and print it
banner_string = banner_bytes.decode('utf-8')
print("Service Banner Received:")
print(banner_string)

## Close the connection
s.close()

Save the file and exit the editor. Now, run your completed script to perform the banner grab.

python3 ~/project/network_basics.py

You should see the connection success message followed by the banner sent by our dummy service:

Successfully connected to 127.0.0.1 on port 8080
Service Banner Received:
Welcome to LabEx Dummy Service v1.0

Summary

In this lab, you learned how to leverage Python's built-in socket library to interact with network services at a low level. You practiced creating a TCP socket, establishing a connection to a specific IP address and port, and receiving raw byte data over the network. By decoding this data, you successfully performed a "banner grab," a fundamental technique used in network reconnaissance to identify running services and their versions.

These foundational skills form the basis for more advanced network programming, custom tool development, and cybersecurity scripting.