8

I have a vpn (using OpenVPN) with mulitple clients connected to a server (at DigitalOcean). The connection is very good and I am able access every client placed behind their respective firewalls when they are connected to their respective routers through ssh. I want to use python scripts to automatically send files from multiple clients to server and vice versa. Here's the code I am using so far:

Server:

#!/usr/bin/env python

import socket
from threading import Thread
from SocketServer import ThreadingMixIn

class ClientThread(Thread):

    def __init__(self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        print "[+] New thread started for "+ip+":"+str(port)


    def run(self):
        while True:
            data = conn.recv(2048)
            if not data: break
            print "received data:", data
            conn.send(data)  # echo

TCP_IP = '0.0.0.0'
TCP_PORT = 62
BUFFER_SIZE = 1024  # Normally 1024


tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []

while True:
    tcpsock.listen(4)
    print "Waiting for incoming connections..."
    (conn, (ip,port)) = tcpsock.accept()
    newthread = ClientThread(ip,port)
    newthread.start()
    threads.append(newthread)

for t in threads:
    t.join()

Client:

#!/usr/bin/env python

import socket


TCP_IP = '10.8.0.1'
TCP_PORT = 62

BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

print "received data:", data

The problem is im not able to get a connection. The server only prints "Waiting for incoming connections..." and the client does not seem to find its way to the server. Is there anyone who can take a look at this and give me some feedback on wath I am doing wrong?

Kolibri
  • 93
  • 1
  • 2
  • 6

1 Answers1

3

Can you try something like this?

import socket
from threading import Thread


class ClientThread(Thread):

    def __init__(self,ip,port):
    Thread.__init__(self)
    self.ip = ip
    self.port = port
    print "[+] New thread started for "+ip+":"+str(port)


def run(self):
    while True:
        data = conn.recv(2048)
        if not data: break
        print "received data:", data
        conn.send(b"<Server> Got your data. Send some more\n")

TCP_IP = '0.0.0.0'
TCP_PORT = 62
BUFFER_SIZE = 1024  # Normally 1024
threads = []

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("0.0.0.0", 5000))
server_socket.listen(10)

read_sockets, write_sockets, error_sockets = select.select([server_socket], [], [])

while True:
    print "Waiting for incoming connections..."
    for sock in read_sockets:
        (conn, (ip,port)) = server_socket.accept()
        newthread = ClientThread(ip,port)
        newthread.start()
        threads.append(newthread)

for t in threads:
    t.join()

Now the client will have something like below:

import socket, select, sys

TCP_IP = '0.0.0.0'
TCP_PORT = 62

BUFFER_SIZE = 1024
MESSAGE = "Hello, Server. Are you ready?\n"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, 5000))
s.send(MESSAGE)
socket_list = [sys.stdin, s]

while 1:
    read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])


    for sock in read_sockets:
        # incoming message from remote server
        if sock == s:
            data = sock.recv(4096)
            if not data:
                print('\nDisconnected from server')
                sys.exit()
            else:
                sys.stdout.write("\n")
                message = data.decode()
                sys.stdout.write(message)
                sys.stdout.write('<Me> ')
                sys.stdout.flush()

        else:
            msg = sys.stdin.readline()
            s.send(bytes(msg))
            sys.stdout.write('<Me> ')
            sys.stdout.flush()

The output is as below:

For server -

Waiting for incoming connections...
[+] New thread started for 127.0.0.1:52661
received data: Hello, World!
Waiting for incoming connections...

received data: Hi!

received data: How are you?

For client -

<Server> Got your data. Send some more
<Me> Hi!
<Me> 
<Server> Got your data. Send some more
<Me> How are you?
<Me> 
<Server> Got your data. Send me more
<Me>

In case you want to have an open connection between the client and server, just keep the client open in an infinite while loop and you can have some message handling at the server end as well. If you need that I can edit the answer accordingly. Hope this helps.

GPrathap
  • 7,336
  • 7
  • 65
  • 83
Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57
  • Thanks for your answer! I tried to run your code, but the connection gets refused. Output for client is: Traceback (most recent call last): File "client_test.py", line 10, in s.connect((TCP_IP, 5000)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused – Kolibri Feb 23 '17 at 12:42
  • Run the server first and then the client. because without the server running, client does not know where to connect to. – Nihal Sharma Feb 23 '17 at 12:52
  • I ran the server first, but it gives the same output: connection refused. Could it be a firewall issue? I can ping both the server local ip: 10.8.0.1 and public ip. But I am not able to telnet the server with either of the ip's.. – Kolibri Feb 23 '17 at 13:01
  • When I configured the openvpn network I used port 443 and I am able to telnet at port 443: telnet 10.8.0.1 443. I also get a blank response if I run the client code using port 443, but I can't run the server code with that port since the port is already in use.. Should that tell me something? – Kolibri Feb 23 '17 at 13:21
  • The port should be open for the client to connect. I am not sure what's the issue you are facing. Open a port on which the server will be running and ask the client to connect to the server on the same port. – Nihal Sharma Feb 23 '17 at 13:27
  • Ok, thank you so much for the respone. I would also be very gratefull if you added the while loop and message handling at server side for continous connection :) – Kolibri Feb 23 '17 at 13:34
  • Thanks for the help! – Kolibri Feb 23 '17 at 15:23
  • Do i even need to use sockets when I already use a vpn network? Should it not be possible to just send data through the already established tunnel with a python script? – Kolibri Feb 23 '17 at 16:33