Is there a python library which implements a standalone TCP stack?
I can\'t use the usual python socket library because I\'m receiving a stream of packets over a socket
Glancing over Scapy, it looks like it might be able to handle these low-level situations. I haven't used it myself so I can't confirm that it does what you've explained; I've only glanced over the documentation.
You might be able to use the ctypes module to import lwip and use it again.
I know this isn't directly Python related but if you are looking to do heavy network processing, you should consider Erlang instead of Python. Just a suggestion really... you can always take a shot a doing this with Twisted... if you feel adventurous (and have lots of time on your side) ;-)
You don't say which platform you are working on, but if you are working on linux, I'd open a tun/tap interface and get the IP packets back into the kernel as a real network interface so the kernel can do all that tricky TCP stuff.
This is how (for example) OpenVPN works - it receives the raw IP packets over UDP or TCP and tunnels them back into the kernel over a tun/tap interface.
I think that there is a tun/tap interface for windows too now which was developed for the OpenVPN port to windows.
If you are already committed to the software at the other end of the socket, that is forwarding TCP packets to you, then perhaps TCPWatch will show you how to get at the SYN packets. SCAPY is certainly great for sending exactly the packets that you want, but I'm not sure that it will work as a proxy.
http://hathawaymix.org/Software/TCPWatch
However, if you are not committed to what is on the sending end, then consider using Twisted Conch or Paramiko to do SSH forwarding. Even if you don't need encryption, you can still use these with blowfish which has a low impact on your CPU. This doesn't mean that you need Conch on the other end, since SSH is standardised so any SSH software should work. In the SSH world this is normally referred to as "port forwarding" and people use an SSH terminal client to log into an SSH server and set up the port forwarding tunnel. Conch and Paramiko allow you to build this into a Python application so that there is no need for the SSH terminal client.