Sockets: code works locally but fails over LAN

Bryan Olson fakeaddress at nowhere.org
Fri Sep 2 00:06:08 EDT 2005


n00m wrote:
 >>Bryan;
 >
 > I tested your code locally (in I*D*L*E) - it works fine!

Glad it worked, but I'd still disrecommend IDLE for that
version. Threads may live after the program seems to be done
(and may still own the port you need). Below is a version that
respects ^C to terminate more-or-less cleanly.

 > And of course I'll test it over LAN but only tomorrow - at work.
 > See the picture of my IDLE window with output of your code:
 > http://free.7host02.com/n00b/socket_Br.gif

I didn't touch the printing, so it should output the same thing
as your version. Looks like you've got some UTF-16 action there,
and Python may be able to print it nicer if you use the
unicode/codec stuff.


--
--Bryan


import socket, threading, select

sqls_host, sqls_port = '192.168.0.3', 1443
proxy_host, proxy_port = '', 1434


def start_deamon_thread(func, args):
      """ Run func(*args) in a deamon thread.
      """
      thread = threading.Thread(target=func, args=args)
      thread.setDaemon(True)
      thread.start()


def sock_copy(s_from, s_to, annotation):
     while 1:
         data = s_from.recv(4096)
         if not data:
             break
         s_to.sendall(data)
         print annotation + data + '\n\n'


s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.bind((proxy_host, proxy_port))
s1.listen(5)

while 1:
     s, _, _ = select.select([s1], [], [], 1.0)
     if s:
         cn, _ = s1.accept()
         s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         s2.connect((sqls_host, sqls_port))
         start_deamon_thread(sock_copy, (cn, s2, 'VB_SCRIPT:'))
         start_deamon_thread(sock_copy, (s2, cn, 'SQL_SERVER:'))



More information about the Python-list mailing list