Home > Enterprise >  Python Flask Redirect on another computer
Python Flask Redirect on another computer

Time:10-28

I'm running a Flask server on my computer, with host = "0.0.0.0"

When I access the server from another computer it works fine, but when I redirect it redirects to "localhost:5000" which doesn't work on the other computer.

Heres my code

    from flask import Flask, url_for, redirect, request, render_template
    import pickle

    app = Flask(__name__)

    @app.route("/")
    def login_page():
        return redirect(url_for("advanced_login_page",prevtry=False))

    @app.route("/login_attempt/<int:prevtry>")
    def advanced_login_page(prevtry):
        return render_template('login.html',prevTry=prevtry)

    @app.route("/login",methods=["POST","GET"])
    def login():
        if request.method == "POST":
            username = request.form["username"]
            password = request.form["password"]
        elif request.method == "GET":
            username = request.args.get("username")
            password = request.args.get("password")

        return redirect(f"/user/{username}")

    @app.route("/user/<username>")
    def home_page(username):
        return "Hello %s" % username

    app.run(host="0.0.0.0")

Heres login.html

    <!doctype html>
    <html>
       <body>
   
           <form action="http://localhost:5000/login" method="post">
              <p>UserName:<input type="text" name="username"/></p>
              <p>Password:<input type="password" name="password"/></p>
              <p><input type="submit" value="login"/></p>
           </form>
           {% if prevTry==True %}
                <p>Incorrect Username or Password</p>
           {% endif %}
      
       </body>
    </html>

I ran the code with host = "0.0.0.0" and then accessed it on my phone, which was connected to the same network. The phone showed the rendered login.html, but when I tried to enter anything and log in, it redirected to "localhost:5000/user/" where username is the entered username. Then it showed "localhost refused to connect"

CodePudding user response:

Think about this for a second:

localhost refers to the current computer. So, on your computer localhost:5000 points to the server running on your computer on port 5000, however, on the other computer, it refers to the server running on another computer on port 5000. The server is not running on the other computer, so the browser freaks out.

The best way to resolve this is to redirect to a host that is available on all machines and refers only to your computer. I don't know how you are accessing your computer from the other computer, but I'll use the hypothetical IP address 192.168.0.10. Remember to change this IP address to however you are actually accessing your computer, whether it is through an IP address or a hostname. Here's an example:

<!doctype html>
<html>

<body>

  <form action="http://192.168.0.10:5000/login" method="post">
     <!--              ^^^^^^^^^^^^ <-- change this        -->
    <p>UserName:<input type="text" name="username" /></p>
    <p>Password:<input type="password" name="password" /></p>
    <p><input type="submit" value="login" /></p>
  </form>
  {% if prevTry==True %}
  <p>Incorrect Username or Password</p>
  {% endif %}

</body>

</html>

This should resolve your problem.

In the real world, you'll want to run a website on a domain (for example, example.com). When you do this, it is best to get the hostname of the current machine, but in your case this is overkill and most likely won't work.

  • Related