Sockets: code works locally but fails over LAN

Bryan Olson fakeaddress at nowhere.org
Fri Sep 2 18:20:22 CEST 2005


I wrote:
 > Below is a version that respects ^C to terminate
 > more-or-less cleanly.

Oops, one more bug^H^H^H improvement. I forgot to shutdown
writing.


 > 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:

Insert:
|             s_to.shutdown(socket.SHUT_WR)

 >             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:'))


-- 
--Bryan



More information about the Python-list mailing list