Flask connection between two machines

Asked

Viewed 1,443 times

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 server machine IP in the client file but I’m not successful. Does anyone have any idea 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()

1 answer

0


Documented on the Flask website under "Externally Visible Server" on the page Quickstart ( 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 default because, in debugging mode, an application user can execute arbitrary Python code on your computer. If you have disabled debug or trust users on your network, you can make the server publicly available.

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()

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

After configuring and still not being able to access, also check if the firewall is not blocking the door 5000.

Browser other questions tagged

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