0

I've got the following app:

server.py

@app.route('/')
    def main():
    return render_template('index.html')

@app.route('/login')
    def login():
    return 'hey'

index.html

<body>
 <form action="http://localhost:5000/login" , method="post">
 <input type="submit" value="submit">
</body>

Now I run ngrok:

ngrok http 5000

After typing address (generated by ngrok) into web browser I can see index.html page with button, but when I press the button it redirects me to http://localhost:5000/login where I can see: "connection refused". My question is how to set ngrok and flask server the way they can communicate?

P.S. I've put only a part of my code just for better reading

Persi
  • 414
  • 6
  • 15
  • Even simpler, use `pyngrok` (`pip install pyngrok`) to invoke and manage `ngrok` right from within your `server.py`. [Here's a full Flask example](https://pyngrok.readthedocs.io/en/latest/integrations.html#flask), but basically you'd just need to `from pyngrok import ngrok` and then `ngrok.connect(5000)` when you're defining the routes. – alexdlaird May 07 '20 at 00:15

4 Answers4

2

Btw, I've figured out how to make it other way. After running command:

ngrok http 5000

I get ngrok address thanks to this python script:

import json
import os

def get_ngrok_address():
    os.system("curl  http://localhost:4040/api/tunnels > tunnels.json")

    with open('tunnels.json') as data_file:
        datajson = json.load(data_file)

return dict(zip(['http', 'https'], [i['public_url'] for i in datajson['tunnels']]))

It just gets json object and converts it to python dict:

'http' -> ngrok_http_address
'https' -> ngrok_https_address

Before server start I pass generated address to all html templates e.x.:

<body>
  <form action="{{ ngrok_address }}/login", method="post">
  <input type="submit" value="submit">
</body>
Persi
  • 414
  • 6
  • 15
0

What happens if you add the POST method to the login route

@app.route('/login', methods=['POST'])
def login():
    return 'hey'

and change the form action to

<body>
 <form action="/login", method="post">
 <input type="submit" value="submit">
</body>

hm?

dimmg
  • 3,316
  • 2
  • 20
  • 29
  • I forgot to write 'POST' here, but it does exist in my code, I'll fix it now. Btw I tried with: action="/login" and it works :) Thank you – Persi Nov 09 '17 at 16:05
0

Try to change your host of application.

app.run(host='0.0.0.0')

and then run the command ngrok http 5000

also add POST method for the your route.

@app.route('/login', methods=['POST'])
def login():
 return 'hey'
  • I forgot to type here 'POST' in my route args, but it exists in my code. Your solution works fine as well. Thank you! – Persi Nov 09 '17 at 16:06
  • Nice! and please mark the answer as correct, thanks! –  Nov 09 '17 at 18:50
0

Try Flask-PyNgrok to use ngrok as flask application extension.