{"article":{"slug":"what-happens-if-you-write-a-tcp-stack-in-python","title":"What happens if you write a TCP stack in Python?","subtitle":null,"summary":"Julia Evans recounts writing a tiny TCP stack in Python with Scapy during Hacker School: doing a handshake with Google, then discovering that the Linux kernel kept resetting her connections and that her Python code was too slow to keep up, and what that taught her about how TCP works.","content_type":"blog_post","language":"en","canonical_url":"https://jvns.ca/blog/2014/08/12/what-happens-if-you-write-a-tcp-stack-in-python/","author":{"name":"Julia Evans","url":null,"person_slug":"julia-evans-2dueqfpm4f1qh","person_url":"https://listedstartups.com/people/julia-evans-2dueqfpm4f1qh"},"authored_by":"human","publisher":{"name":"Julia Evans","url":"https://jvns.ca/","listing_slug":null,"listing":null},"topics":[{"name":"networking","slug":"networking","url":"https://listedarticles.com/topics/networking"},{"name":"python","slug":"python","url":"https://listedarticles.com/topics/python"},{"name":"tcp","slug":"tcp","url":"https://listedarticles.com/topics/tcp"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":1081,"reading_minutes":5,"published_at":"2014-08-12T08:52:30.000Z","added_at":"2026-11-16T07:50:20.569Z","updated_at":"2026-11-16T07:50:20.569Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/what-happens-if-you-write-a-tcp-stack-in-python","markdown_url":"https://listedarticles.com/articles/what-happens-if-you-write-a-tcp-stack-in-python.md","example":false,"citation":"Julia Evans, Julia Evans. \"What happens if you write a TCP stack in Python?.\" 12 Aug 2014. https://jvns.ca/blog/2014/08/12/what-happens-if-you-write-a-tcp-stack-in-python/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://jvns.ca/blog/2014/08/12/what-happens-if-you-write-a-tcp-stack-in-python/"},"body_markdown":"# What happens if you write a TCP stack in Python?\n\n•\n[hackerschool](https://jvns.ca/categories/hackerschool) •\n[how-things-work](https://jvns.ca/categories/how-things-work) •\n[python](https://jvns.ca/categories/python) •\n[favorite](https://jvns.ca/categories/favorite) •\n\nAugust 12, 2014\n\nDuring Hacker School, I wanted to understand networking better, and I\ndecided to write a miniature TCP stack as part of that. I was much\nmore comfortable with Python than C and I’d recently discovered the\n[scapy](http://www.secdev.org/projects/scapy/) networking library\nwhich made sending packets\n[really easy](http://jvns.ca/blog/2013/10/31/day-20-scapy-and-traceroute/).\n\nSo I started writing [teeceepee](https://github.com/jvns/teeceepee)!\n\nThe basic idea was\n\n1. open a raw network socket that lets me send TCP packets\n2. send a HTTP request to `GET` google.com\n3. get and parse a response\n4. celebrate!\n\nI didn’t care much about proper error handling or anything; I just\nwanted to get one webpage and declare victory :)\n\n## [Step 1: the TCP handshake](#step-1-the-tcp-handshake)\n\nI started out by doing a TCP handshake with Google! (this won’t\nnecessarily run correctly, but illustrates the principles). I’ve\ncommented each line.\n\nThe way a TCP handshake works is:\n\n- me: SYN\n- google: SYNACK!\n- me: ACK!!!\n\nPretty simple, right? Let’s put it in code.\n\n```\n# My local network IP\nsrc_ip = \"192.168.0.11\"\n# Google's IP\ndest_ip = \"96.127.250.29\"\n# IP header: this is coming from me, and going to Google\nip_header = IP(dst=dest_ip, src=src_ip)\n# Specify a large random port number for myself (59333),\n# and port 80 for Google The \"S\" flag means this is\n# a SYN packet\nsyn = TCP(dport=80, sport=59333, \n          ack=0, flags=\"S\")\n# Send the SYN packet to Google\n# scapy uses '/' to combine packets with headers\nresponse = srp(ip_header / syn)\n# Add the sequence number \nack = TCP(dport=80, sport=self.src_port, \n          ack=response.seq, flags=\"A\") \n# Reply with the ACK\nsrp(ip_header / ack)\n```\n\n### [Wait, sequence numbers?](#wait-sequence-numbers)\n\nWhat’s all this about sequence numbers? The whole point of TCP is to\nmake sure you can resend packets if some of them go missing. Sequence\nnumbers are a way to check if you’ve missed packets. So let’s say that\nGoogle sends me 4 packets, size 110, 120, 200, and 500 bytes. Let’s\npretend the initial sequence number is 0. Then those packets will have\nsequence numbers 0, 110, 230, and 430.\n\nSo if I suddenly got a 100-byte packet with a sequence number of 2000,\nthat would mean I missed a packet! The next sequence number should be\n930!\n\nHow can Google know that I missed the packet? Every time I receive a\npacket from Google, I need to send an ACK (“I got the packet with\nsequence number 230, thanks!”). If the Google server notices I haven’t\nACKed a packet, then it can resend it!\n\nThe TCP protocol is extremely complicated and has all kinds of rate\nlimiting logic in it, but we’re not going to talk about any of that.\nThis is all you’ll need to know about TCP for this post!\n\nFor a more in-depth explanation, including how SYN\npackets affect sequence numbers, I found\n[Understanding TCP sequence numbers](http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/)\nvery clear.\n\n## [Step 2: OH NO I already have a TCP stack](#step-2-oh-no-i-already-have-a-tcp-stack)\n\nSo I ran the code above, and I had a problem. IT DIDN’T WORK.\n\nBut in a kind of funny way! I just didn’t get any responses. I looked\nin Wireshark (a wonderful tool for spying on your packets) and it\nlooked like this:\n\n```\nme: SYN\ngoogle: SYNACK\nme: RST\n```\n\nWait, what? I never sent a `RST` packet?! `RST` means STOP THE\nCONNECTION IT’S OVER. That is not in my code at all!\n\nThis is when I remembered that I *already* have a TCP stack on my\ncomputer, in my kernel. So what was actually happening was:\n\n```\nmy Python program: SYN\ngoogle: SYNACK\nmy kernel: lol wtf I never asked for this! RST!\nmy Python program: ... :(\n```\n\nSo how do we bypass the kernel? I talked to the delightful\n[Jari Takkala](https://github.com/jtakkala) about this, and he\nsuggested using\n[ARP spoofing](http://jvns.ca/blog/2013/10/29/day-18-in-ur-connection/)\nto pretend I had a different IP address (like `192.168.0.129`).\n\nThe new exchange was like this:\n\n```\nme: hey router! send packets for 192.168.0.129 to my MAC address\nrouter: (does it silently)\nmy Python program: SYN (from 192.168.0.129)\ngoogle: SYNACK\nkernel: this isn't my IP address! <ignore>\nmy Python program: ACK YAY\n```\n\nAnd it worked! Okay, awesome, we can now send packets AND GET\nRESPONSES without my kernel interfering! AWESOME.\n\n## [Step 3: get a webpage!](#step-3-get-a-webpage)\n\nThere is an intervening step here where I fix tons of irritating bugs\npreventing Google from sending me the HTML for <http://google.com>. I\neventually fixed all of these, and emerge victorious!\n\nI needed to\n\n- put together a packet containing a HTTP GET request\n- make sure I can listen for *lots* of packets in response, not just\n  one\n- spend a lot of time fixing bugs with sequence numbers\n- try to close the connection properly\n\n## [Step 4: realize Python is slow](#step-4-realize-python-is-slow)\n\nOnce I had everything working, I used Wireshark again to look at what\npackets were being sent back and forth. It looked something like this:\n\n```\nme/google: <tcp handshake>\nme: GET google.com\ngoogle: 100 packets\nme: 3 ACKs\ngoogle: <starts resending packets>\nme: a few more ACKs\ngoogle: <reset connection>\n```\n\nThe sequences of packets from Google and ACKs from me looked something\nlike: P P P A P P P P P A P P A P P P P A. Google was sending me\npackets *way* faster than my program could keep up and send ACKs.\nThen, hilariously, Google’s server would assume that there were\nnetwork problems causing me to not ACK its packets.\n\nAnd it would eventually reset the connection because it would decide\nthere were connection problems.\n\nBut the connection was fine! My program was totally responding! It was\njust that my Python program was way too slow to respond to packets in\nthe millisecond times it expected.\n\n(edit: this diagnosis seems to be incorrect :) you can\n[read some discussion](https://news.ycombinator.com/item?id=8167546)\nabout what may be actually going on here)\n\n## [life lessons](#life-lessons)\n\nIf you’re actually writing a production TCP stack, don’t use Python.\n(surprise!) Also, the TCP spec is really complicated, but you can get\nservers to reply to you even if your implementation is extremely sketchy.\n\nI was really happy that it actually worked, though! The ARP spoofing\nwas extremely finicky, but I wrote a version of `curl` using it which\nworked about 25% of the time. You can see all the absurd code at\n<https://github.com/jvns/teeceepee/>.\n\nI think this was actually way more fun and instructive than trying to\nwrite a TCP stack in an appropriate language like C :)\n\n[Pair programming is amazing! Except... when it's not.](https://jvns.ca/blog/2014/08/05/pair-programming-is-amazing-except-when-its-not/ \"Previous Post: Pair programming is amazing! Except... when it's not.\")\n[How is a binary executable organized? Let's explore it!](https://jvns.ca/blog/2014/09/06/how-to-read-an-executable/ \"Next Post: How is a binary executable organized? Let's explore it!\")\n","body_html":"<h1 id=\"what-happens-if-you-write-a-tcp-stack-in-python\">What happens if you write a TCP stack in Python?</h1>\n<p>•\n<a href=\"https://jvns.ca/categories/hackerschool\" rel=\"nofollow ugc noopener\">hackerschool</a> •\n<a href=\"https://jvns.ca/categories/how-things-work\" rel=\"nofollow ugc noopener\">how-things-work</a> •\n<a href=\"https://jvns.ca/categories/python\" rel=\"nofollow ugc noopener\">python</a> •\n<a href=\"https://jvns.ca/categories/favorite\" rel=\"nofollow ugc noopener\">favorite</a> •</p>\n<p>August 12, 2014</p>\n<p>During Hacker School, I wanted to understand networking better, and I\ndecided to write a miniature TCP stack as part of that. I was much\nmore comfortable with Python than C and I’d recently discovered the\n<a href=\"http://www.secdev.org/projects/scapy/\" rel=\"nofollow ugc noopener\">scapy</a> networking library\nwhich made sending packets\n<a href=\"http://jvns.ca/blog/2013/10/31/day-20-scapy-and-traceroute/\" rel=\"nofollow ugc noopener\">really easy</a>.</p>\n<p>So I started writing <a href=\"https://github.com/jvns/teeceepee\" rel=\"nofollow ugc noopener\">teeceepee</a>!</p>\n<p>The basic idea was</p>\n<ol><li>open a raw network socket that lets me send TCP packets</li><li>send a HTTP request to <code>GET</code> google.com</li><li>get and parse a response</li><li>celebrate!</li></ol>\n<p>I didn’t care much about proper error handling or anything; I just\nwanted to get one webpage and declare victory :)</p>\n<h2 id=\"step-1-the-tcp-handshake\"><a href=\"#step-1-the-tcp-handshake\">Step 1: the TCP handshake</a></h2>\n<p>I started out by doing a TCP handshake with Google! (this won’t\nnecessarily run correctly, but illustrates the principles). I’ve\ncommented each line.</p>\n<p>The way a TCP handshake works is:</p>\n<ul><li>me: SYN</li><li>google: SYNACK!</li><li>me: ACK!!!</li></ul>\n<p>Pretty simple, right? Let’s put it in code.</p>\n<pre><code># My local network IP\nsrc_ip = &quot;192.168.0.11&quot;\n# Google&#39;s IP\ndest_ip = &quot;96.127.250.29&quot;\n# IP header: this is coming from me, and going to Google\nip_header = IP(dst=dest_ip, src=src_ip)\n# Specify a large random port number for myself (59333),\n# and port 80 for Google The &quot;S&quot; flag means this is\n# a SYN packet\nsyn = TCP(dport=80, sport=59333, \n          ack=0, flags=&quot;S&quot;)\n# Send the SYN packet to Google\n# scapy uses &#39;/&#39; to combine packets with headers\nresponse = srp(ip_header / syn)\n# Add the sequence number \nack = TCP(dport=80, sport=self.src_port, \n          ack=response.seq, flags=&quot;A&quot;) \n# Reply with the ACK\nsrp(ip_header / ack)</code></pre>\n<h3 id=\"wait-sequence-numbers\"><a href=\"#wait-sequence-numbers\">Wait, sequence numbers?</a></h3>\n<p>What’s all this about sequence numbers? The whole point of TCP is to\nmake sure you can resend packets if some of them go missing. Sequence\nnumbers are a way to check if you’ve missed packets. So let’s say that\nGoogle sends me 4 packets, size 110, 120, 200, and 500 bytes. Let’s\npretend the initial sequence number is 0. Then those packets will have\nsequence numbers 0, 110, 230, and 430.</p>\n<p>So if I suddenly got a 100-byte packet with a sequence number of 2000,\nthat would mean I missed a packet! The next sequence number should be\n930!</p>\n<p>How can Google know that I missed the packet? Every time I receive a\npacket from Google, I need to send an ACK (“I got the packet with\nsequence number 230, thanks!”). If the Google server notices I haven’t\nACKed a packet, then it can resend it!</p>\n<p>The TCP protocol is extremely complicated and has all kinds of rate\nlimiting logic in it, but we’re not going to talk about any of that.\nThis is all you’ll need to know about TCP for this post!</p>\n<p>For a more in-depth explanation, including how SYN\npackets affect sequence numbers, I found\n<a href=\"http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/\" rel=\"nofollow ugc noopener\">Understanding TCP sequence numbers</a>\nvery clear.</p>\n<h2 id=\"step-2-oh-no-i-already-have-a-tcp-stack\"><a href=\"#step-2-oh-no-i-already-have-a-tcp-stack\">Step 2: OH NO I already have a TCP stack</a></h2>\n<p>So I ran the code above, and I had a problem. IT DIDN’T WORK.</p>\n<p>But in a kind of funny way! I just didn’t get any responses. I looked\nin Wireshark (a wonderful tool for spying on your packets) and it\nlooked like this:</p>\n<pre><code>me: SYN\ngoogle: SYNACK\nme: RST</code></pre>\n<p>Wait, what? I never sent a <code>RST</code> packet?! <code>RST</code> means STOP THE\nCONNECTION IT’S OVER. That is not in my code at all!</p>\n<p>This is when I remembered that I <em>already</em> have a TCP stack on my\ncomputer, in my kernel. So what was actually happening was:</p>\n<pre><code>my Python program: SYN\ngoogle: SYNACK\nmy kernel: lol wtf I never asked for this! RST!\nmy Python program: ... :(</code></pre>\n<p>So how do we bypass the kernel? I talked to the delightful\n<a href=\"https://github.com/jtakkala\" rel=\"nofollow ugc noopener\">Jari Takkala</a> about this, and he\nsuggested using\n<a href=\"http://jvns.ca/blog/2013/10/29/day-18-in-ur-connection/\" rel=\"nofollow ugc noopener\">ARP spoofing</a>\nto pretend I had a different IP address (like <code>192.168.0.129</code>).</p>\n<p>The new exchange was like this:</p>\n<pre><code>me: hey router! send packets for 192.168.0.129 to my MAC address\nrouter: (does it silently)\nmy Python program: SYN (from 192.168.0.129)\ngoogle: SYNACK\nkernel: this isn&#39;t my IP address! &lt;ignore&gt;\nmy Python program: ACK YAY</code></pre>\n<p>And it worked! Okay, awesome, we can now send packets AND GET\nRESPONSES without my kernel interfering! AWESOME.</p>\n<h2 id=\"step-3-get-a-webpage\"><a href=\"#step-3-get-a-webpage\">Step 3: get a webpage!</a></h2>\n<p>There is an intervening step here where I fix tons of irritating bugs\npreventing Google from sending me the HTML for <a href=\"http://google.com\" rel=\"nofollow ugc noopener\">http://google.com</a>. I\neventually fixed all of these, and emerge victorious!</p>\n<p>I needed to</p>\n<ul><li>put together a packet containing a HTTP GET request</li><li><p>make sure I can listen for <em>lots</em> of packets in response, not just</p><p>one</p></li><li>spend a lot of time fixing bugs with sequence numbers</li><li>try to close the connection properly</li></ul>\n<h2 id=\"step-4-realize-python-is-slow\"><a href=\"#step-4-realize-python-is-slow\">Step 4: realize Python is slow</a></h2>\n<p>Once I had everything working, I used Wireshark again to look at what\npackets were being sent back and forth. It looked something like this:</p>\n<pre><code>me/google: &lt;tcp handshake&gt;\nme: GET google.com\ngoogle: 100 packets\nme: 3 ACKs\ngoogle: &lt;starts resending packets&gt;\nme: a few more ACKs\ngoogle: &lt;reset connection&gt;</code></pre>\n<p>The sequences of packets from Google and ACKs from me looked something\nlike: P P P A P P P P P A P P A P P P P A. Google was sending me\npackets <em>way</em> faster than my program could keep up and send ACKs.\nThen, hilariously, Google’s server would assume that there were\nnetwork problems causing me to not ACK its packets.</p>\n<p>And it would eventually reset the connection because it would decide\nthere were connection problems.</p>\n<p>But the connection was fine! My program was totally responding! It was\njust that my Python program was way too slow to respond to packets in\nthe millisecond times it expected.</p>\n<p>(edit: this diagnosis seems to be incorrect :) you can\n<a href=\"https://news.ycombinator.com/item?id=8167546\" rel=\"nofollow ugc noopener\">read some discussion</a>\nabout what may be actually going on here)</p>\n<h2 id=\"life-lessons\"><a href=\"#life-lessons\">life lessons</a></h2>\n<p>If you’re actually writing a production TCP stack, don’t use Python.\n(surprise!) Also, the TCP spec is really complicated, but you can get\nservers to reply to you even if your implementation is extremely sketchy.</p>\n<p>I was really happy that it actually worked, though! The ARP spoofing\nwas extremely finicky, but I wrote a version of <code>curl</code> using it which\nworked about 25% of the time. You can see all the absurd code at\n<a href=\"https://github.com/jvns/teeceepee/\" rel=\"nofollow ugc noopener\">https://github.com/jvns/teeceepee/</a>.</p>\n<p>I think this was actually way more fun and instructive than trying to\nwrite a TCP stack in an appropriate language like C :)</p>\n<p><a href=\"https://jvns.ca/blog/2014/08/05/pair-programming-is-amazing-except-when-its-not/\" title=\"Previous Post: Pair programming is amazing! Except... when it&#39;s not.\" rel=\"nofollow ugc noopener\">Pair programming is amazing! Except... when it&#39;s not.</a>\n<a href=\"https://jvns.ca/blog/2014/09/06/how-to-read-an-executable/\" title=\"Next Post: How is a binary executable organized? Let&#39;s explore it!\" rel=\"nofollow ugc noopener\">How is a binary executable organized? Let&#39;s explore it!</a></p>","headings":[{"level":1,"text":"What happens if you write a TCP stack in Python?","id":"what-happens-if-you-write-a-tcp-stack-in-python"},{"level":2,"text":"Step 1: the TCP handshake","id":"step-1-the-tcp-handshake"},{"level":3,"text":"Wait, sequence numbers?","id":"wait-sequence-numbers"},{"level":2,"text":"Step 2: OH NO I already have a TCP stack","id":"step-2-oh-no-i-already-have-a-tcp-stack"},{"level":2,"text":"Step 3: get a webpage!","id":"step-3-get-a-webpage"},{"level":2,"text":"Step 4: realize Python is slow","id":"step-4-realize-python-is-slow"},{"level":2,"text":"life lessons","id":"life-lessons"}]}}