route to Flask encounters 404 - flask

I'm following the steps in https://github.com/twilio/starter-python
Access to http://localhost:5000/ can get expected response though http://localhost:5000/hello just gets 404.
Here part of the code
from flask import Flask, Response, request, render_template
from twilio.twiml.voice_response import VoiceResponse
from twilio.rest import Client
#app.route('/hello', methods=['GET', 'POST'])
def hello():
response = VoiceResponse()
response.say('Hello there! You have successfully configured a web hook.')
response.say('Good luck on your Twilio quest!', voice='woman')
return Response(str(response), mimetype='text/xml')
I actually run the example on there python app.py, the only modification is three system environment variables the app needed.
I can't figure what could cause that. Could someone give a clue?

I had the same problem before. But mine was solved as some other application was running on port 5000. Try changing the port and I think it should work.
Try referring to this stack overflow

Related

Configuring a Flask application running on gunicorn to run behind a file path [duplicate]

This question already has answers here:
Python - Flask Default Route possible?
(4 answers)
Closed 7 months ago.
I am trying to configure a Flask application hosted with gunicorn to run behind a file path.
The current app is designed to run at www.example.com, but I would like to configure it to run at example.com/foo/bar
The difficulty I am having is that I can't seem to get the requests to be sent to the application. It currently works fine if you go to example.com, but going to example.com/foo/bar results in nothing. Everything is done through AWS. The networking has been setup to pass example.com/foo/bar requests to the server. The Flask init is as follows:
import os
from functools import wraps
import requests
from flask import Flask, render_template, make_response, redirect, Response, g
app = Flask(__name__)
app.config['VERSION'] = '3.7.2'
app.config['SERVER_ADDRESS'] = os.environ.get('SERVER_ADDRESS', 'https://example.com/')
app.config['APPLICATION_ROOT'] = '/foo/bar/'
#app.before_request
def set_server_address():
g.server_address = app.config['SERVER_ADDRESS']
#app.route('/')
def index(*args, **kwargs):
return redirect('/app/example')
When I check the logs after visiting example.com/foo/bar, there is no traffic. Any light which could be shed on this would be very much appreciated.
You can solve this problem by adding the /foo/bar to your flask route.
#app.route('/foo/bar')
def make_homepage():
return 'success'
But it's not best practice because each time the response will be as a redirect and with high traffic it's could be pretty resource intensive, so i will recommend to you to check this question:
Python - Flask Default Route possible?

Problems serving a dash webapp in an internal network?

I have the following problem:
I'm trying to serve a dash app in my internal network.
from dash import Dash, html
app = Dash(__name__)
app.layout = hmtl.Div([html.H1('hello dash'), html.Div('test')])
app.run_server(host='0.0.0.0', port=8050, debug=False)
The above doesn't work - meaning I can only access it from the host machine when navigating to the browser. Other computers on the local network get an error saying:
Network admin couldn't see that there were any issues so I wrote a similar min server but running on flask to see if the issue was Dash and indeed the following is reachable just fine from a different machine on the network:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'asdf'
app.run(host='0.0.0.0', port=8050)
And indeed this works just fine. Any idea what might be going on?
I saw this comment here which seemed to indicate someone experienced some issues due to browser settings?
Error while running Dash app locally, "This site can’t be reached"
I was trying to access this using chrome

flask url_for behind a reverse proxy

I have a flask application running on a server (192.168.1.1:8080) located behind a reverse proxy. Let's say, the url https://foo.bar.com/myapp point to my flask app, i.e. to 192.168.1.1:8080.
I am in trouble with the url_for flask function, as it returns something like http://192.168.1.1:8080/blabla (giving blabla as parameter).
How to proceed so that url_for returns https://foo.bar.com/myapp/blabla instead ?
In fact, my application uses a CAS authentication system. So that the ticket gets validate by the CAS server, I need to provide an URL of the shape https://foo.bar.com/....
Any help would be appreciate.
I found a solution using ProxyFix from werkzeug :
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app, x_host=1)

Deploying a Flask Web Application: Access from Remote Server

I am building a small web app with Flask so that someone else can use my script from their device (in network) without installing the attendant modules. I can get the app to run on my localhost but am having trouble getting it to be available to other devices in network.
I'm using Flask's in-built platform so I am using app.run(host="0.0.0.0"). I've tried enabling/disabling the debug parameter. I've tried adding the port parameter as 5000, although it already seems to be running on that port. I've had the other person go to http://my_ip_address:5000 as well as http://0.0.0.0:5000 (not sure which one they should be using).
Below is a segment of my code, with the actual app classes and methods not included:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/')
def command_outputs():
return render_template("info.html")
#app.route('/result',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
f=request.files["File"]
f.save(f.filename)
f.stream.seek(0)
finder=OutputFinder(result["Device"], f, result["Username"], result["Password"])
finder.findOutput()
lines=finder.result.split("\n")
return "<br/>".join(lines)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)
When I run the script (I am using PyCharm) and enter http://0.0.0.0:5000 into the browser, I get an "Unknown Host" error. When I enter the localhost or my device's IP address, I get a 404 not found error, and 127.0.0.1 - - [26/Jun/2019 15:03:42] "GET / HTTP/1.1" 404 appears in the terminal.
When the other person opens it with http://0.0.0.0:5000, they get the same error I do. When they run it using my IP address, they get a "site cannot be reached" page.
I'm pretty sure I'm just putting the wrong stuff into the URL. Also, the error messages are inconsistent sometimes. I will run the exact same script and get differing error messages, but the ones mentioned above are the most common.
EDIT: It is working on my own computer when I use my IP address in the url, but it is not working on the other person's computer using my IP address.

Can I get the JSON response from a Flask-api that runs locally using URLLIB or REQUETS?

Is there a way to save the response of a flask api that is running locally on my machine?
It may not make a great sense as I have the logic locally and there is no need to get the response again from the local URL..but in my case, I have another webhook which runs locally which means I need to run flask and my webhook locally.
I am looking to get around this..
You can save your response on your machine through using pickle. But it is not recommended, because website can change their content anytime.
import requests
import pickle
resp = requests.get("https://github.com")
with open("test","wb") as fd:
pickle.dump(resp,fd)
with open("test","rb") as fd:
resp_ = pickle.load(fd)
print(resp_.url)