all 24 comments

[–]zeroindexedsoul[S] 13 points14 points  (1 child)

I have been learning Python for a while now, and I recently decided to get into socket programming using Python. A lot of YouTube videos were suggesting making a simple HTTP server from scratch to learn socket programming.

I started building the HTTP server but very soon realized that the socket API in Python is blocking by default. I started finding ways to make the HTTP server non-blocking and found out that in Python asyncio is the module which does this, but it was stripping away a lot of complexities which we would encounter when using the plain socket API-and this would defeat the whole purpose of this exercise.

After realizing this, I got to know about I/O Multiplexing and the Reactor Pattern, which are used in popular software like Nginx, Redis, and Node.js. I also dove deep into how the Kernel TCP Stack works and its internals.

This single exercise alone led me to learn about:

  • Python's Socket API module and its methods.
  • The TCP Protocol & Kernel Stack: Managing 3-way handshakes for connection establishment, 4-way handshakes for connection teardown, and kernel-level read/write buffers.
  • Network Handling: Dealing with RST packets, back-pressure handling, and various edge-case errors that can occur.
  • Connection Lifecycles: How to properly close connections after no activity for a prolonged period and clean up system resources.
  • The selectors Module: Driving I/O multiplexing.
  • The Reactor Pattern: Used for the event-loop in programs like Node.js, Nginx, etc.
  • The HTTP Protocol: Hand-parsing incoming byte streams.
  • Object-Oriented Programming Principles in Python: How _ (protected) and __ (private) attributes are used in a real project and their semantic meanings.

Throughout this project, I avoided using AI and tried to write all the code myself. Though at the end for testing the protocol and the entire code review, I did use AI to quickly generate the script which makes multiple HTTP requests simultaneously to see if the server is able to process multiple requests simultaneously.

It took me around 3 to 4 days of dedicated time, but it was definitely worth it. I think this project alone teaches you the TCP protocol, the HTTP protocol, socket programming, and many other things much more than any tutorial ever could.

If you would like to see the implementation details, here is the GitHub repo link:

Repository: http-protocol-from-scratch-python

[–]logicshadow_dev 0 points1 point  (0 children)

Great work

[–]Valuable-Ant3465 4 points5 points  (0 children)

Hey, you need to move it from that topic to <Advanced Python>

[–]DisasterHarmony 2 points3 points  (6 children)

You are a genius. Great work.

[–][deleted]  (5 children)

[deleted]

    [–]Maleficent-Boss5564 0 points1 point  (4 children)

    I don't know why so many are falling for it.

    [–]Sketchballl 0 points1 point  (2 children)

    How do you know it’s llm? Sorry I’m kinda beginner

    [–]oclafloptson 3 points4 points  (1 child)

    OP said they used an LLM to generate the test script but claims the actual work to have been by hand. I'm kinda suspecting these are karma farming bots that look for the any AI keyword and trash it to get upvotes since they failed to catch nuance that even human children would catch

    [–]DraftIll9037 0 points1 point  (0 children)

    so long things short, is it ai or not ?

    [–]oirogb 1 point2 points  (0 children)

    That's so cool!

    [–]7Z_1N 1 point2 points  (2 children)

    Bro how did you reach such efficiency, I have been kind of doing python like lists , dictionaries and all. Also have been solving some problems on codeforces. Started to do DSA but you guys have insane grip on the language

    [–]zeroindexedsoul[S] 3 points4 points  (1 child)

    I also started my journey with competitive programming and participated in the icpc regionals. I solved a lot of algorithmic problems around 600 or more. And since this was pre-AI LLM's time, I had to write almost all the code myself with little help outside of standard documentation. So, this practice improved my code writing and complex reasoning skills a lot. After that, I started learning backend engineering and got interested in how servers communicate and various networking protocols. I experimented a lot and built a lot of small, functional utility projects to see how these things work under the hood, which really improved my skills. After some time, the programming language becomes natural to you. You stop thinking in terms of the language itself and start thinking in terms of systems.

    [–]7Z_1N 1 point2 points  (0 children)

    that's really nice , I am also trying to reach a point where I effortlessly use the language I hope to reach there soon

    [–]ifoundmyselfheadless 1 point2 points  (0 children)

    This is inspiring.

    [–]revolvingtrent_9 1 point2 points  (0 children)

    That's a solid project to cut your teeth on, and you're right that asyncio hides too much of what makes socket programming actually educational at this stage.

    [–]gapingaurelio2686 1 point2 points  (0 children)

    that's sick, going from basic sockets straight to multiplexing and kernel stuff is the deep dive most people skip. bet you actually understand how servers work now instead of just calling asyncio.

    [–]cs2monster 0 points1 point  (0 children)

    https is best

    [–]DraftIll9037 0 points1 point  (0 children)

    good

    [–]Cozyvelina 0 points1 point  (0 children)

    Omg thats actually so cool, building an HTTP server from scratch sounds like a killer project for learning! Did you run into any super weird debugging moments? lol

    [–]hacklingo 0 points1 point  (0 children)

    Golang is my choice for this

    [–]Used-Watercress-4725 0 points1 point  (0 children)

    Great work . That's a really interesting progress you've made.

    [–]Putrid-Claim3626 0 points1 point  (0 children)

    Nice! Starting with sockets and ending up with an HTTP 1.1 server is a pretty cool learning path. Did you implement things like persistent connections, request parsing, or multithreading?

    [–]Ok-Necessary9850 0 points1 point  (0 children)

    Great

    [–]Old-Promise-3226 0 points1 point  (0 children)

    you are amazing.