3

I have these two python scripts.

The server :

#!/bin/env python

#--*-- coding:UTF-8 --*--

import socket
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = input("Ip : ")
port = int(input("Port : "))
connexion.connect((host, port))
print("The connection with {} is done.".format(host, port))
started = True

while started:
    choice = int(input(" [1] Command\n [2] Leave\n > "))
    if choice == 1:
        command = input(" Command : ")
        connexion.send(command.encode())
        retour = connexion.recv(1024).decode('latin1')
        print(retour)

        if choice == 2:
            connexion.send("exit")
            connexion.close()
            break

And the client :

#!/bin/env python

#--*-- coding:UTF-8 --*--

import socket
import subprocess

connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 631
connexion.bind((host,port))
connexion.listen(5)
connexion_client, infos_connexion = connexion.accept()
started = True

while started:
    rcv_cmd = connexion_client.recv(1024)
    rcv_cmd = rcv_cmd.decode()
    if rcv_cmd != "exit":
        cmd = subprocess.Popen(rcv_cmd, shell=true, stdout = subprocess.PIPE, stderr =subprocess.PIPE, stdin = subprocess.PIPE )
        out = cmd.stdout.read() + cmd.sdterr.read()
        connexion_client.send(out)
    else:
        started = False
        conexion.close()
        exit

These two scripts work perfectly on my local network, but if I want to use it on two different networks, how can I do that? Do you have any link/tips on how to learn how to do that?

3
  • Do you mean across two subnets that you host locally? Or do you mean between two different public IPs? Commented Sep 9, 2019 at 18:51
  • 1
    I mean two different public IPs :) Commented Sep 9, 2019 at 20:03
  • cool! So what you would need to do is called port forwarding Commented Sep 9, 2019 at 20:23

1 Answer 1

2

To achieve data transfer between those two servers, you will need to port forward the computer running the server. To do this, you need to have access to your router's control panel. Then the instructions vary depending on your router, but you need to tell the router to forward the server's local IP and port to the public IP and port.

After that, you should be able to connect to your server from the client using the public IP of the server's router.

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

Comments

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.