0

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.

1
  • Anyone else have any suggestions? I'm stuck on creating and reading from array of sockets that listen on random ports. Commented Sep 28, 2017 at 1:16

1 Answer 1

1

Look at the select function. It does what you want, but does so without multithreading. Instead, it lets you wait for "any socket," and then react to it. There are many tutorials on how to use it.

Sign up to request clarification or add additional context in comments.

16 Comments

Thanks. I did start looking at Select. I guess where I'm confused is with the forwarding part. For example if I have a client connect to server via telnet and it connects on remote port 64233, I want to be able to connect from another client and send and receive data with only client one.Or if I have vnc server running on client 1, be able to connect to server via vnc viewer and send data to the socket of client 1
@ABarnes It sounds like it may be worth your time to look into some tutorials on how sockets work. What you describe is what will happen naturally if you use sockets the way people use sockets.
Maybe I'm not explaining it well enough. I certainly am aware of how sockets work and function. I'm new to python. The issue is if my client is already connected on a socket (always connected) and lets say the remote port is 64233, how can I connect to server and tell it to send the data to client connected on 64233 and not other clients connected. Quick example is I have a server setup, 3 clients connected. each has vnc server running on them. I connect to server with vnc viewer and want to control Client 2. I will need to fwd data to socket that client 2 is connected on.
@ABarnes Usually you have a port mapping (in a file). For example, there might be a line that says any data that comes in on port 1234 is forwarded to 192.168.1.2:2345. Then whenever someone connects to the server on port 1234, it opens a new socket to port 2345 on 192.168.1.2 and begins forwarding.
Thanks but here is the issue with that. Server is external running python server script, listening on port 1234. Client 1 connects from remote LAN to server port 1234. Server shows Client 1 is connected on port 64233. Now I would like Client 2 to be able to connect to server port 64233 and send data directly to Client 1. I have a method to get connected port, but can't figure out how to have server listen on a different port for each connection. Keep in mind this could have many connections
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.