Sockets: code works locally but fails over LAN
Bryan Olson
fakeaddress at nowhere.org
Wed Aug 31 17:03:19 EDT 2005
n00m wrote:
> import socket, thread
> host, port = '192.168.0.3', 1434
Consider using INADDR_ANY instead of the specific host IP
address. The empty string will resolve to INADDR_ANY if passed
as the host to bind(). (Though that's not the problem.)
> 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()
You probably want the the accept() in a loop.
> def VB_SCRIPT():
> while 1:
> data = cn.recv(4096)
> if not data: return
> s2.send(data)
Use sendall(), not send().
> print 'VB_SCRIPT:' + data + '\n\n'
>
> def SQL_SERVER():
> while 1:
> data = s2.recv(4096)
> if not data: return
> cn.send(data)
As above, use sendall().
> print 'SQL_SERVER:' + data + '\n\n'
>
> thread.start_new_thread(VB_SCRIPT,())
> thread.start_new_thread(SQL_SERVER,())
>
> =============================================
>
> The above code acts as an intermediator between a very simple VB script
> and SQL Server. Like follows:
> the vbs sends its requests to "fake" port 1434 and the python code
> catches and re-sends them to sql server which listens to its DEFAULT
> port = 1433... And vice versa.
[...]
> It works fine (I see all client/server data printed in IDLE window)but
> only locally. I.e. if vbs, python and sql server run on the same
> machine.
[...]
My first two guess are:
The client is trying to make more than one connection.
Putting accept in a loop and moving some stuff will support
that.
The send() call is not sending all the data. Using sendall()
should fix that.
Try the following, but *not in Idle*. The code doesn't yet have
a clean stopping method, and the run-on threads will screw-up
Idle.
import socket, thread
sqls_host, sqls_port = '192.168.0.3', 1443
proxy_host, proxy_port = '', 1434
# How I tested it:
# sqls_host, sqls_port = 'www.google.com', 80
def VB_SCRIPT():
while 1:
data = cn.recv(4096)
if not data: return
s2.sendall(data)
print 'VB_SCRIPT:' + data + '\n\n'
def SQL_SERVER():
while 1:
data = s2.recv(4096)
if not data: return
cn.sendall(data)
print 'SQL_SERVER:' + data + '\n\n'
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.bind((proxy_host, proxy_port))
s1.listen(5)
while 1:
cn, addr = s1.accept()
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((sqls_host, sqls_port))
thread.start_new_thread(VB_SCRIPT,())
thread.start_new_thread(SQL_SERVER,())
--
--Bryan
More information about the Python-list
mailing list