Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access localhost from another computer on same network? [duplicate]

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".

like image 622
VVK kumar Avatar asked Jan 21 '26 03:01

VVK kumar


2 Answers

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

like image 153
Sabith Avatar answered Jan 22 '26 15:01

Sabith


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

like image 43
Dave W. Smith Avatar answered Jan 22 '26 16:01

Dave W. Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!