Kod: Zaznacz cały
Unhandled exception in thread started by
Traceback (most recent call last):
File "./tunnel.py", line 46, in Recv
self.CloseTunnel()
File "./tunnel.py", line 24, in CloseTunnel
self.plugins.sock.shutdown(1)
File "<string>", line 1, in shutdown
socket.error: (107, 'Transport endpoint is not connected')
Kod: Zaznacz cały
#!/usr/bin/python
from thread import start_new_thread
from socket import *
LEN_RECV = 262144
in_addr = '0.0.0.0'
in_port = 80
out_addr = '127.0.0.1'
out_port = 81
drukuj = 'nie'
class new_plug_in:
def __init__(self):
self.sock = 0
self.send_to = 1
self.plugins = []
self.description = ''
def CloseTunnel(self):
self.plugins[self.send_to].sock.shutdown(1)
self.plugins[self.send_to].sock.close()
def Send(self, data):
try:
self.sock.send(data)
except:
self.CloseTunnel()
def Recv(self):
while(1):
try:
data = self.sock.recv(LEN_RECV)
if(len(data) == 0):
self.CloseTunnel()
return
if(drukuj == 'tak'):
print self.description
print HexDump(data)
return
self.plugins[self.send_to].Send(data)
except:
self.CloseTunnel()
return
def Run(self):
if(len(self.plugins) == 0):
self.plugins.append(self)
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((out_addr, out_port))
except:
s.close()
self.sock.close()
return
tunnel_out = new_plug_in()
tunnel_out.sock = s
tunnel_out.send_to = 0
self.plugins.append(tunnel_out)
self.description = '[CLIENT]'
tunnel_out.description = '[SERVER]'
tunnel_out.plugins = self.plugins
tunnel_out.Run()
start_new_thread(self.Recv, ())
def HexDump(data):
a = -1
out = '-' * 59 + '\n'
out += '0x00000000: '
for i in range(0, len(data)):
a += 1
if(a == 16):
out += '\n0x%08x: ' % (i)
a = 0
out += '%02x ' % (ord(data[i]))
out += '\n' + ('-' * 59)
return out
def AcceptConnect(cl, addr):
if(drukuj == 'tak'):
print "Connection accepted from: %s" % (addr[0])
return
tunnel_in = new_plug_in()
tunnel_in.sock = cl
tunnel_in.Run()
def InitServer(bind_addr, bind_port):
s = socket(AF_INET, SOCK_STREAM)
s.bind((bind_addr, bind_port))
print "Listen on %s:%d..." % (bind_addr, bind_port)
s.listen(1)
while(1):
cl, addr = s.accept()
start_new_thread(AcceptConnect, (cl, addr,))
s.close()
InitServer(in_addr, in_port)