HomeBlogs
Logo
Hamburger

Day1:Building a Python Firewall

My First Step Towards Cybersecurity – Building a Python Firewall

pyfirewall

As I embark on my cybersecurity journey, I’ve decided to document everything I learn. You can check out my previous blog here.



Creating Python Firewall

Today, I started working on a home firewall in Python to learn about sockets, networking, and how programs interact with the OSI model. I began by exploring Python’s socket module to create a basic client-server communication system.



What I Built

  1. A custom Python server using sockets

  2. A manual Python client to send requests to the server

  3. Implemented basic data exchange between client and server.

Python socket



Key Learnings

✅ How to create and bind a socket to a port
✅ How to listen for incoming connections
✅ Handling concurrent connections using socket.listen()
✅ Sending and receiving data between client and server
✅ Understanding raw sockets and how they work
✅ Debugging network issues using netstat


Debugging Lessons

While running multiple tests, I encountered an issue where my server wouldn’t restart😅. Turns out, the port was already in use due to previous runs 😁. I learned to use netstat to find and kill processes blocking my port.😎


MY BAD


Breaking Down My Code:

serverCode



Server Side (server.py)

1️⃣ Import the socket module:

import socket



2️⃣ Create a socket:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  • AF_INET → IPv4 addressing
  • SOCK_STREAM → TCP connection (alternative: SOCK_DGRAM for UDP)



3️⃣ Bind the socket to an IP and port:

sock.bind(("0.0.0.0", 8080))



4️⃣ Start listening for connections:

sock.listen(5) 



5️⃣ Accept incoming connections:

conn, client_IP = sock.accept()


6️⃣ Send data to the client:

conn.send("Hello, this is the server".encode("utf-8"))

7️⃣ Close the connection:

conn.close()


clientCode



Client Side (client.py)

1️⃣ Create a client socket:

sock = socket.socket()



2️⃣ Connect to the server:

sock.connect(("SERVER_IP", 8080))



3️⃣ Receive server response:

data = sock.recv(2048)
print(data.decode("utf-8"))



4️⃣ Close the client connection:

sock.close()


Final Thoughts:



This might not seem like a big achievement, but for me, it’s a step closer to my dream: becoming a Security Engineer. Every small step matters!



Here is a little reference if you want to:



Soon i will upload my progress on github for cleaner documentation.

Looking forward to continuing this journey—see you in the next blog! 🚀

Happy Programming, Happy Hacking🧑‍💻

Published on: Sat Mar 01 2025

Explore my other blogs

Hire Me For Your Next Amazing Project

Let’s make somthing new, different and more meaningful or make thing more visual or conceptual? Just say hello!

--- +91-9812914813

--- himanshusharma2719@gmail.com

--- Yamuna Nagar, Haryana, India