[Twisted-Python] Re: Sockets: code works locally but fails over LAN

Jp Calderone exarkun at divmod.com
Wed Aug 31 10:51:08 EDT 2005

On 31 Aug 2005 06:03:00 -0700, n00m <n00m at narod.ru> wrote:
>import socket, thread
>host, port = '', 1434
>s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>s2.connect((host, 1433))
>s1.bind((host, port))
>cn, addr = s1.accept()
>def VB_SCRIPT():
>    while 1:
>        data = cn.recv(4096)
>        if not data: return
>        s2.send(data)
>        print 'VB_SCRIPT:' + data + '\n\n'
>def SQL_SERVER():
>    while 1:
>        data = s2.recv(4096)
>        if not data: return
>        cn.send(data)
>        print 'SQL_SERVER:' + data + '\n\n'

This is about the same as:

    mktap portforward --port 1434 --host --dest_port 1433
    twistd -f portforward.tap

You'll find the code behind these two commands here:


and here:


And of course, the main Twisted site is <http://twistedmatrix.com/>.

Some differences between portforward.tap and your code include:

portforward.tap will accept multiple connections, rather than just one.  portforward.tap won't print out all the bytes it receives (I assume this is just for debugging purposes anyway - if not, a simple modification will cause it to do this).  portforward.tap won't non-deterministically drop traffic, since Twisted checks the return value of send() and properly re-transmits anything which has not actually been sent.

Hope this helps,


More information about the Twisted-Python mailing list