I'm trying to build an application with my RPi 3B that get some data from connected sensors. Once I got the data, I'd like to send them to a computer that is on an external network via a script in Python and the socket library. Here is my code:
On server side (the external computer):
import socket
ip = (([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")]
or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]])
+ ["no IP found"])[0]
print("IP ordinateur local: {}".format(ip))
port = 9999
print("Port d'entrée des données sur l'ordinateur local: {}".format(port))
address = ("",port)
serveur = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serveur.bind(address)
serveur.listen(5)
print("En attente de nouvelles connexions")
client, infosClient = serveur.accept()
print("Nouvelle connexion client établie")
print("Infos client: IP = {}, port = {}".format(infosClient[0],infosClient[1]))
msg = "Serveur: connexion acceptee"
client.send(msg.encode())
flag = False
try:
while True:
if flag == False:
msgRecu = client.recv(1024)
print("Message reçu: {}".format(msgRecu.decode()))
msg = input("Entrez une réponse à envoyer \n")
client.send(msg.encode())
print("Réponse envoyée")
print("En attente de la commande d'arrêt")
flag = True
except KeyboardInterrupt:
msg = "Le serveur a enclenché la commande d'arret Ctrl+C"
client.send(msg.encode())
finally:
print("Fermeture de la connexion")
client.close()
serveur.close()
On client side (the RPi):
import socket
ipLocal = (([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")]
or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]])
+ ["no IP found"])[0]
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ipServeur = input("Entrez l'adresse IP du serveur \n")
portServeur = input("Entrez le port du serveur \n")
try:
client.connect((ipServeur,int(portServeur)))
except KeyboardInterrupt:
del client
print("Connexion établie")
msgRecu = client.recv(1024)
print(msgRecu.decode())
msg = input("Entrez un message à envoyer \n")
client.send(msg.encode())
print("Message envoyé")
answer = client.recv(1024)
print("Réponse serveur: {}".format(answer.decode()))
print("Fermeture de la connexion")
client.close()
Unfortunately when I enter the IP address that the server script gives me via the first command (ip = ...
), the client script tells me I'm connected but the server one is still waiting for a connection.
I think it could possibly come from the fact that the ip address I get from the command is a local one, but I don't know how to get the external, especially since the network of the server is well protected. A friend told me to use a VPN but I don't know much things about how it works, any idea ?
Update info from comment:
After further discussion it appears the only solution would be to create a VPN. The difficulty here is that the server network is managed and protected by a a subcontracting company.
So the question is: how to create a Virtual Private Network (VPN) for this conditions?