将 Flask 和 Ngrok 用于 http://127.0.0.1:5000 时找不到 404
404 Not Found when using Flask and Ngrok for http://127.0.0.1:5000
flask、ngrok 和 twilio 的完全初学者。我 运行 这段 python 代码为 twilio 创建了一个烧瓶应用程序:
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def incoming_sms():
"""Send a dynamic reply to an incoming text message"""
# Get the message the user sent our Twilio number
body = request.values.get('Body', None)
# Start our TwiML response
resp = MessagingResponse()
# Determine the right reply for this message
if body == 'hello':
resp.message("Hi!")
elif body == 'bye':
resp.message("Goodbye")
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
然后我 运行 "ngrok http http://127.0.0.1:5000" 创建了一个 ngrok link,但是当我使用它时我得到 404 not found。是什么导致了这个问题,我怎么能修复它?
更新:http://127.0.0.1:5000/sms 不起作用,显示空白页
1. Run ngrok 5000
2. Here is the flask code for generating, sending and validating OTP:
@app.route('/getOTP', methods=['GET', 'POST'])
def getOTP():
mobNum = request.get_json().get("mobNum")
length = len(mobNum)
val = 0
if length == 13 :
val = getOTPApi(mobNum)
elif length == '' or length < 12:
message = "Failure"
print(message)
return message
{'ContentType':'application/json'}
if val:
message = "Success"
print(message)
return message
else:
message = "NaN"
print(message)
return message
@app.route('/validateOTP', methods=['POST'])
def validateOTP():
otp = request.get_json().get("otp")
length = len(otp)
if 'response' in session:
s = session['response']
if s == otp:
message = "Success"
print(message)
return message
elif length == 0 or length < 6:
message = "NaN"
print(message)
return message
else:
message = "Failure"
print(message)
return message
session.pop('response', None)
def generateOTP():
return random.randrange(100000, 999999)
def getOTPApi(mobNum):
account_sid = 'XXXxxXXXXXX'
auth_token = 'XXXxxXXXXXX'
client = Client(account_sid, auth_token)
otp = generateOTP()
session['response'] = str(otp)
body = 'Your OTP is ' + str(otp)
message = client.messages.create(
from_='+1123456789',
body=body,
to=mobNum
)
if message.sid:
return True
else:
return False
flask、ngrok 和 twilio 的完全初学者。我 运行 这段 python 代码为 twilio 创建了一个烧瓶应用程序:
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def incoming_sms():
"""Send a dynamic reply to an incoming text message"""
# Get the message the user sent our Twilio number
body = request.values.get('Body', None)
# Start our TwiML response
resp = MessagingResponse()
# Determine the right reply for this message
if body == 'hello':
resp.message("Hi!")
elif body == 'bye':
resp.message("Goodbye")
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
然后我 运行 "ngrok http http://127.0.0.1:5000" 创建了一个 ngrok link,但是当我使用它时我得到 404 not found。是什么导致了这个问题,我怎么能修复它?
更新:http://127.0.0.1:5000/sms 不起作用,显示空白页
1. Run ngrok 5000
2. Here is the flask code for generating, sending and validating OTP:
@app.route('/getOTP', methods=['GET', 'POST'])
def getOTP():
mobNum = request.get_json().get("mobNum")
length = len(mobNum)
val = 0
if length == 13 :
val = getOTPApi(mobNum)
elif length == '' or length < 12:
message = "Failure"
print(message)
return message
{'ContentType':'application/json'}
if val:
message = "Success"
print(message)
return message
else:
message = "NaN"
print(message)
return message
@app.route('/validateOTP', methods=['POST'])
def validateOTP():
otp = request.get_json().get("otp")
length = len(otp)
if 'response' in session:
s = session['response']
if s == otp:
message = "Success"
print(message)
return message
elif length == 0 or length < 6:
message = "NaN"
print(message)
return message
else:
message = "Failure"
print(message)
return message
session.pop('response', None)
def generateOTP():
return random.randrange(100000, 999999)
def getOTPApi(mobNum):
account_sid = 'XXXxxXXXXXX'
auth_token = 'XXXxxXXXXXX'
client = Client(account_sid, auth_token)
otp = generateOTP()
session['response'] = str(otp)
body = 'Your OTP is ' + str(otp)
message = client.messages.create(
from_='+1123456789',
body=body,
to=mobNum
)
if message.sid:
return True
else:
return False