3

I have written some flask api and running on http://localhost:5000/ . I want to test those api's from other computer which is connected in a same network. I am using windows 10.

What I have tried yet:

  1. I did ipconfig/all and got ipv4 address . Wireless LAN adapter Wi-Fi:-> IPv4 Address: XX.XX.XXX.XXX(Preferred).

  2. Ran XX.XX.XXX.XXX:5000 , but got "This site can’t be reached".

Sabith
  • 1,628
  • 2
  • 20
  • 38
VVK kumar
  • 267
  • 3
  • 5
  • 15

4 Answers4

3

Open the port 5000 in your machine. Follow the steps:

  1. Navigate to Control Panel, System and Security and Windows Firewall.
  2. Select Advanced settings and highlight Inbound Rules in the left pane.
  3. Right click Inbound Rules and select New Rule.
  4. Add the port you need to open and click Next(5000 in your case).
  5. Add the protocol (TCP or UDP) and the port number into the next window and click Next.
  6. Select Allow the connection in the next window and hit Next.
  7. Select the network type as you see fit and click Next.
  8. Name the rule something meaningful and click Finish.

Then try to access through <your ip>:5000

sample code is as follows:

import flask

app = flask.Flask(__name__)
app.config["DEBUG"] = True


@app.route('/', methods=['GET'])
def home():
    return "<h1>Test Data</p>"

app.run(host='0.0.0.0')

and run your application through cmd as python api.py where api.py is the file name

Sabith
  • 1,628
  • 2
  • 20
  • 38
2

To access a Flask app from another machine, you need the app to bind to 0.0.0.0 instead of localhost (127.0.0.1). The latter won't route to another machine.

If you're running the app via python, use run(host='0.0.0.0').

If you're running the app the flask run, add --host=0.0.0.0

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • "sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Cannot assign requested address" got this error by changing localhost to 0.0.0.0 – VVK kumar Nov 25 '19 at 06:40
1

You can simply type the IP address of the host into your browser with the specified port number and you're good to go as both the computers are in the same network.

Juhil Somaiya
  • 873
  • 7
  • 20
-1

Might be easier to just make it public temporarily: https://ngrok.com/

But would be even easier to do your testing locally from the same machine.

minboost
  • 2,555
  • 1
  • 15
  • 15