I am trying to come up with a solution for a python tcp socket server. It needs to handle multiple connections, but here's the catch I'm stuck on. When the client connects I can see the remote port assigned and can communicate via other clients connected. I want to be able to connect with a client and send data to specific socket/ Client. Basically what I'm trying to accomplish is I have a client behind a firewall. I connect to the socket server on an internet based server. Now I would like to connect to the same server with a different client and send all data through server based on destination port. I know I can do this with ssh2 tunnelling with forwarding, but I'm trying to come up with a way natively in python. Important to note the clients are not python based. Could be anything. For example, vnc on computer. connection to server. Remote vnc viewer connects to server and forwards to already connected socket. Any help would be greatly appreciated.
This is a sample I have so far.
import socket, select
sock_config = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_config.bind(('0.0.0.0', 5000))
sock_config.listen(5)
config = []
rlist = [sock_config]
wlist = []
errlist = []
out_buffer = []
while True:
r, w, err = select.select(rlist, wlist, errlist)
for sock in r:
if sock == sock_config:
conf, addr = sock.accept()
config.append(conf)
rlist.append(conf)
else:
data = sock.recv(1024)
if data.find('[CONNECT]') != -1:
sock_clients = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_clients.bind(('0.0.0.0', 12345))
sock_clients.listen(2)
sock.send("12345")
sock.close()
rlist.remove(sock)
else:
out_buffer.append(data)
out_string = ''.join(out_buffer)
out_buffer = []
#for sock in w:
# print 'Test'
for sock in err:
sock.close()
rlist.remove(sock)
Can't figure out how to listen to random ports and still be able to read and write to them. There will be multiple clients on random ports.