Deploying a Flask Web Application: Access from Remote Server - flask

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.

Related

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 API, browser requests stopped working, no log the request was received

I have a very simple flask app that has been working for years, but last week requests from the built app return a 500, and from the Flask side, I can't even see the request. I am not seeing an OPTIONS request.
The below lines worked previously to keep CORS happy.
#app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization,Auth-Token')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
I have tried in a few browsers and all of them fail to successfully make any requests. Since the server doesn't even acknowledge the request has been made, I am not sure where to trouble shoot. I did confirm the app returns data as expected when I use Postman to make the request, as well as confirming that if I use the app locally (gulp serve on my computer) that the requests are successful. I have to believe its CORS, but what might I have to add / do to get the browser to be happy? Thanks.
The solution to my problem was that chrome started to "restrict the ability of websites to communicate with devices on the local network"
Communicating from Chrome 94+ with LAN devices that do not support HTTPS from a web app

route to Flask encounters 404

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

Using Bokeh Server in Google Cloud AI Notebook

I am attempting to build and test some bokeh components in the Google Cloud AI notebook environment. I have been using this cloud instance for several months with little to no issues but I cannot seem to get the tutorial, https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html, working. Has anyone successfully worked with a bokeh server in this environment?
The tutorial does not seem to work for my use case. Several JavaScript errors are returned when following and adapting this method.
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(modify_doc, notebook_url=remote_jupyter_proxy_url)
def remote_jupyter_proxy_url(port):
"""
Callable to configure Bokeh's show method when a proxy must be
configured.
If port is None we're asking about the URL
for the origin header.
"""
base_url = URL OF AI NOTEBOOK
host = urllib.parse.urlparse(base_url).netloc
# If port is None we're asking for the URL origin
# so return the public hostname.
if port is None:
return host
service_url_path = NOT A JUPYTER HUB SO UNCLEAR WHAT SHOULD BE HERE
proxy_url_path = 'proxy/%d' % port
user_url = urllib.parse.urljoin(base_url, service_url_path)
full_url = urllib.parse.urljoin(user_url, proxy_url_path)
print(full_url)
return full_url
When patching together the previous code, I receive the following message when inspecting the notebook:
panellayout.js:213 Mixed Content: The page at 'URL' was loaded over HTTPS, but requested an insecure script 'URL'. This request has been blocked; the content must be served over HTTPS.

Connect to dev server in Flask for unittest

I am trying to write unittests for my Flask API endpoints. I want the test cases to connect to the dev server which is running on a different port 5555.
Here is what I am doing in setUp() to make a test_client.
import flask_app
flask_app.app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://root:#localhost/mvp_test_db'
flask_app.app.config['TESTING'] = True
flask_app.app.config['SERVER_NAME'] = '192.168.2.2' //the IP of the dev server
flask_app.app.config['SERVER_PORT'] = 5555
self.app_client = flask_app.app.test_client()
Then when I make a request using app_client like -
r = self.app_client.post('/API/v1/dummy_api', data = {'user_id' : 1})
I get a 404 when I print r and the request never comes to the dev server (no logs printed). I am not able to inspect the URL to which the connection is being attempted above. Any ideas?
It (app.test_client) does not send requests through network interfaces. All requests are simulated. These are processed inside werkzeug routing system. To process "/API/v1/dummy_api" url you need register a view for. If it is registered, connect it in the import section. Application settings and test settings almost always have almost equal settings.