"local" server with python, does not receive an external POST on the same network

Asked

Viewed 62 times

2

I have a server running on Windows 7 PC and am using Pycharm IDE to edit and simulate. This server is receiving information from an ESP32 module that is connected on the same PC network. Sending the ESP32 module to the server:

Serial.println("========= Inicio post para servidor ==============");  
http.begin(client, Send_http_server(dia,temperatura, umidade, pressao, altitude, RPM, str_biruta, est_motor1, flag_motor1, pluviometro)); //HTTP
http.addHeader("Content-Type", "application/json");//inclui um cabeçalho da aplicação http 
int  httpCode = http.POST(Send_http_server(dia,temperatura, umidade, pressao, altitude, RPM, str_biruta, est_motor1, flag_motor1, pluviometro));
String payload = http.getString(); //Get the response payload
http.end();//finaliza a conexão alivia o sistema
Serial.print("Valor recebido do Rest httpCode:");
Serial.println(httpCode); //imprime o valor recebido do servidor
Serial.println("Play Load:" + payload); //Print request response payload
get_http(httpCode);  
Serial.println("========= Fim post para servidor ==============");

On the server I have:

    @app.route('/dados_esp32/<string:dados>', methods=['GET', 'POST'])
def parse_request(dados):
    if request.method == 'POST':
       bla.bla .bla....

In the first tests the sending happened all right, my server received and processed; from one hour to another the server failed to receive the POST. I tried several things without success, I tried the firewall and still does not receive the POST.

But when I send a request from the server to the ESP32 module they communicate without error. In order for the system to work as intended, the module must trigger uploads to the server and not the other way around.

Including more information:

Tela do PC

  • Try immediately before the if request.method == 'POST' place print(request.method) to confirm which method is being received. Another thing is to add all methods in the parameter methods from Developer. Another thing: how are you lifting the Flask server? Finally, try netstat -an | find /i "listening" to see open doors and listening to connections.

  • Hello Paulo Marques, thank you for your attention! I tried the alternatives indicated but without result yet, I tried to create a rule for the port 5000 in the firewall, I could not release the access.

  • Do not put greetings and thanks in the question. Also do not include issues unrelated to IT, the system informs us that it is a new user.

  • Analyzing the question, there is only one fragment of the error message, it is necessary to see completely to initialize the analysis of which leads the server to refuse the connection. What’s in the log file 2021-03-04.txt? Do not publish code or log as image

1 answer

3

You are unable to receive an external post because you are not binding with the machine ip, http://0.0.0.0:5000.

The configuration must be done in the local instance of flask to set an initial value for the host parameter of the run function, by default when this value is not set the value is used '127.0.0.1'.

app py.

# python version 3.8.2
import os
from flask import json
from flask import Flask, render_template
from dotenv import load_dotenv
load_dotenv()

app = Flask(_name_)
app.config['API_KEY'] = os.getenv("APIKEY")
    
# configurações de rotas

if _name_ == '_main_':
    app.run(host='0.0.0.0', debug=False) #<-- esta configuração deve ser feita, setar host='0.0.0.0'

Another possibility is to configure via command line when serving applications.

$ flask run --host=0.0.0.0

Information on parameters that can be used in flask applications is part of Quickstart of the flask.

  • Ufa! seemed to be complicated... Danizavtz my system did not support the installation of dotenv, however set to app.run(host='0.0.0.0', debug=False); released the communication. But I continue with the doubt because it was working with app.run(debug=True, host='127.0.0.1', port=5000) and stopped overnight???

  • The dotenv is not necessary, unfortunately I also do not know why it worked at some point, according to the logs that posted only we see that is not working, the only thing I can conclude is that maybe you have made a post 'local' so it worked.

Browser other questions tagged

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