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

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


On 31 Aug 2005 06:03:00 -0700, n00m <n00m at narod.ru> wrote:
>import socket, thread
>host, port = '192.168.0.3', 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))
>s1.listen(1)
>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'
>
>thread.start_new_thread(VB_SCRIPT,())
>thread.start_new_thread(SQL_SERVER,())

This is about the same as:

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

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

<http://cvs.twistedmatrix.com/cvs/trunk/twisted/tap/portforward.py?view=markup&rev=13278>

and here:

<http://cvs.twistedmatrix.com/cvs/trunk/twisted/protocols/portforward.py?view=markup&rev=12914>

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,

Jp




More information about the Twisted-Python mailing list