Flask connection between two machines

0

I have a Client-Server application in Flask, running the two files on the same machine the connection works perfectly, but when I try to run the client on a different machine, the connection is not established. I put the IP of the server machine in the client file but I do not succeed. Anyone know what to do?

Server:

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

todos = {}

valores = {}

contador = 0

class TodoSimple(Resource):

    def get(self, todo_id):
        #return {todo_id: todos[todo_id]}

        return valores[todo_id]

    def put(self, todo_id):
        global contador, valores
        contador+=1

        #print(contador)

        todos[todo_id] = request.form['data']

        if contador==4:
            contador = 0
            valores['CPF'] = todos['CPF']
            valores['Latitude'] = todos['Latitude']
            valores['Longitude'] = todos['Longitude']
            valores['Velocidade'] = todos['Velocidade']


        return {todo_id: todos[todo_id]}

api.add_resource(TodoSimple, '/<string:todo_id>')

if __name__ == '__main__':
    app.run(debug=False)

Client:

from requests import put

put('http://localhost:5000/CPF', data={'data': '12345678910'}).json()
put('http://localhost:5000/Latitude', data={'data': '-3.10'}).json()
put('http://localhost:5000/Longitude', data={'data': '-20.4000'}).json()
put('http://localhost:5000/Velocidade', data={'data': '60.5'}).json()
    
asked by anonymous 12.09.2017 / 22:02

1 answer

0

Documented on the Flask site under "Externally Visible Server" on the Quickstart page ( in English).

  

Externally Visible Server

     

If you run the server, you will notice that the server is only available   on your own computer and not on any other network. This is the   because in debug mode an application user can   run the arbitrary Python code on your computer. If you have   disabled debug or trust users on your network, you can   publicly available the server.

     

Just change the run () method call to look like this:

     

app.run (host = '0.0.0.0')

     

This tells your operating system to listen on a public IP.

Server:

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False)

Client:

put('http://0.0.0.0:5000/CPF', data={'data': '12345678910'}).json()
put('http://0.0.0.0:5000/Latitude', data={'data': '-3.10'}).json()
put('http://0.0.0.0:5000/Longitude', data={'data': '-20.4000'}).json()
put('http://0.0.0.0:5000/Velocidade', data={'data': '60.5'}).json()

Change 0.0.0.0 by the IP address of the server, change in the localhost client by the IP of the server.

After setting up and still can not access, also check that firewall is not blocking the 5000 port.

    
13.09.2017 / 02:31