How to make my python server work on an external network using ngrok?

Asked

Viewed 105 times

3

I created a server in python that runs on port 50000... Soon after that I created my client. If the two are on the same network both connect and everything is fine! But in external network I can not make the connection with both. Then I discovered a tool called ngrok, downloaded it and started using it. I am doing it as follows: I run my server on port 50000 after that I run the following command: ngrok.exe http 50000 ngrok works, opens the link and beauty, but when I enter the generated link my server made in python connects with what I think is ngrok, because it returns me this:

Host: 66907fd55f8a.ngrok.io
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
Sec-Ch-Ua: "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
Sec-Ch-Ua-Mobile: ?0
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Upgrade-Insecure-Requests: 1
X-Forwarded-For: Meu ip
X-Forwarded-Proto: https


Digite o comando: 

But I want my client to connect to the server so I can send commands to it.

The short code of the server and the client, it is summarized because it is very long, because in both codes I added several functions: py server.:

import socket
from time import sleep

def abrir_servidor():
    bind_ip = ''  # local onde o servidor roda
    # bind_ip = ''
    bind_port = 50000  # porta onde roda o servidor

    print('Iniciando servidor: ')
    sleep(0.5)

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((bind_ip, bind_port))
    server.listen(1)

    print('Servidor iniciado!')

    print('Aguardando cliente...')
    global conexao
    global endereco
    conexao, endereco = server.accept()
    print(f'Conectado em: {endereco}')
def main():
    abrir_servidor()
    while True:
        global comandos
        comandos = input('Digite o comando: ')
        


while True:
    main()

py client.:

import socket
import os

def conectar():
    #conectando no servidor:
    target_host = 'localhost'
    target_port = 50000
    global cliente
    cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    cliente.connect((target_host, target_port))
    # localizar script:
    for a in range(1):
        cliente.send(os.getcwd().encode())
        
def main():
    conectar()
    while True:
        global comandos
        comandos = cliente.recv(4000).decode()
        
while True:
    main()

How do I do this? I’m already running out of ideas. Is there another way to make my server run on an external network?

  • Can you edit the question and put the server code or, if it is too big, a [mcve]? It is easier to try to respond if there is a problem in the code and not in the infrastructure (network, firewall, etc).

  • I just edited the question by putting the client and server code. Thanks for the tip to add them, I had even forgotten

  • When you say "on the same network", do you mean on the same computer? Or on different computers connected by a network? The client connection is pointing to 'localhost', which suggests that the two programs are running on the same machine. The initial server code is correct, only the first message (os.getcwd) and is missing the sending (to client) of the typed commands.

  • Same network I say both on the same machine and on different computers connected on the same wifi network. In the code this localhost because I was testing on my pc. About receiving the message (os.getcwd) already arranged. But I still don’t know how to get my server ready to receive clients on an external network.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.