Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python flask+ngrok-重定向不起作用_Python_Http_Flask_Ngrok - Fatal编程技术网

Python flask+ngrok-重定向不起作用

Python flask+ngrok-重定向不起作用,python,http,flask,ngrok,Python,Http,Flask,Ngrok,我有以下应用程序: 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"> <inpu

我有以下应用程序:

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>
在网络浏览器中输入ngrok生成的地址后,我可以看到带有按钮的index.html页面,但当我按下按钮时,它会将我重定向到我可以看到的位置:连接被拒绝。 我的问题是如何设置ngrok和flask服务器的通信方式


另外,我只放了一部分代码,只是为了更好地阅读

如果将POST方法添加到登录路径中会发生什么

@app.route('/login', methods=['POST'])
def login():
    return 'hey'
并将表单更改为action

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

嗯?

尝试更改应用程序的主机

app.run(host='0.0.0.0')
然后运行命令ngrok http5000

还为您的路线添加POST方法

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

顺便说一句,我已经想出了另一种方法。运行命令后:

ngrok http 5000
由于这个python脚本,我获得了ngrok地址:

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']]))
它只是获取json对象并将其转换为python dict:

'http' -> ngrok_http_address
'https' -> ngrok_https_address
在服务器启动之前,我将生成的地址传递给所有html模板e.x:

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

尝试使用ngrok作为flask应用程序扩展。

我忘了在这里写“POST”,但它确实存在于我的代码中,我现在将修复它。顺便说一句,我尝试了:action=/login,它可以工作:谢谢。我忘记在我的路由参数中键入“POST”,但它存在于我的代码中。您的解决方案也很有效。非常感谢。美好的请将答案标记为正确,谢谢!更简单的是,使用pyngrok pip install pyngrok从server.py中调用和管理ngrok,但在定义路由时,基本上只需要从pyngrok导入ngrok,然后再导入ngrok.connect5000。
<body>
  <form action="{{ ngrok_address }}/login", method="post">
  <input type="submit" value="submit">
</body>