how to use flask driver in python splinter - python

all my code is just:
from splinter import Browser
from flask import Flask, request
from splinter.driver.flaskclient import FlaskClient
app = Flask(__name__)
browser = Browser('flask', app=app)
browser.visit('https://www.google.com')
print(browser.html)
which print the 404 html:
404 Not Found
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
there is anything I should do?

You are getting a 404 error because your Flask app has no routes.
I believe the purpose of the Splinter Flask client is to test your Flask app, not to test/request other domains. Visiting another domain with the Splinter Flask client will simply request the URL from your domain. You have not specified any routes for your Flask app, so Flask is responding with a 404 error.
Here's an example that shows how the Splinter Flask client is working:
# define simple flask app
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
#app.route('/<name>')
def hello_world(name):
return 'Hello, {name}!'.format(name=name)
# initiate splinter flask client
from splinter import Browser
browser = Browser('flask', app=app)
# simple flask app assertions
browser.visit('http://127.0.0.1:5000')
assert browser.html == 'Hello, World!'
browser.visit('http://127.0.0.1:5000/haofly')
assert browser.html == 'Hello, haofly!'
# Notice that requesting other domains act as if it's your domain
# Here it is requesting the previously defined flask routes
browser.visit('http://www.google.com')
assert browser.html == 'Hello, World!'
browser.visit('http://www.google.com/haofly')
assert browser.html == 'Hello, haofly!'
Here's another test that demonstrates what's really going on:
from flask import Flask
app = Flask(__name__)
#app.errorhandler(404)
def page_not_found(e):
return 'Flask 404 error!', 404
from splinter import Browser
browser = Browser('flask', app=app)
browser.visit('http://www.google.com/haofly')
assert browser.html == 'Flask 404 error!'

Related

Cant run flask on ngrok

from flask import Flask, escape, request
app = Flask(__name__)
run_with_ngrok()
#app.route('/')
def hello():
name = request.args.get("name", "World")
return f'Hello, {escape(name)}!'
When I run the this from terminal with "flask run" it doesn't print an ngrok link.
Im i an virtual env and i have tried running it with python "file name" and it did not work.
if you are trying to expose your ip through ngrok, you can try tunneling with ngrok on terminal for the flask app's port
your app code should look like :
from flask import Flask, escape, request
app = Flask(__name__)
#app.route('/')
def hello():
name = request.args.get("name", "World")
return f'Hello, {escape(name)}!'
if __name__ == "__main__":
app.run(port=5000)
you can tunnel the flask app port with the following command:
ngrok http 5000
here the port 5000 denotes the flask app port.
I think you forgot to add this part to end of your file
if __name__ == "__main__":
app.run()
from flask_ngrok import run_with_ngrok
from flask import Flask, escape, request
app = Flask(__name__)
app.secret_key = '33d5f499c564155e5d2795f5b6f8c5f6'
run_with_ngrok(app)
#app.route('/')
def hello():
name = request.args.get("name", "World")
return f'Hello, {escape(name)}!'
if __name__ == "__main__":
app.run(debug=True)
We can grab token from ngrok.com website by signin
In terminal we need to run like
ngrok config add-authtoken <your_token>
ngrok http 5000
for flask it is 5000 and for other application it would be different
And we also need to run our application side by side

How to redirect in aws app runner with flask

I am trying to deploy my flask application to aws app runner, locally everything works perfectly. But I can't figure out how to redirect to another page of my website in app runner
My code looks similar to this
from flask import Flask, url_for
from waitress import serve
app = Flask(__name__)
#app.route("/hello")
def hello():
return "Hello"
#app.route("/redirect_to")
def redirect_to():
return "Redirected successfully!"
#app.route("/redirect_from")
def redirect_from():
return redirect(url_for("redirect_to"))
if __name__ == "__main__":
serve(app, host="0.0.0.0", port=8000)
App runner provided "Default domain" that redirects all traffic to my app, that is running on 0.0.0.0:8000. When I request default-domain.awsapprunner.com/hello, it successfully redirects to 0.0.0.0:8000/hello, but when I try to request default-domain.awsapprunner.com/redirect_from page loads forever. I think it happens because my app redirects to 0.0.0.0, and app runner expects that all traffic comes to default-domain.awsapprunner.com but I am not sure
What is the best way to fix this problem?
from flask import Flask, url_for, redirect
from waitress import serve
app = Flask(__name__)
#app.route("/hello")
def hello():
return "Hello"
#app.route("/redirect_to")
def redirect_to():
return "Redirected successfully!"
#app.route("/redirect_from")
def redirect_from():
return redirect("http://YOUR_APP_URL.com/redirect_to")
if __name__ == "__main__":
serve(app, host="0.0.0.0", port=8000)

Is there a fix for 404 not found with flask?

When I run this code it gives a "404 page not found" error
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/Home')
def home():
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True)
You should set the route to home as just "/"
#app.route('/')
def home():
...
or visit /Home in your browser
Try changing the last line to app.run(host='0.0.0.0') and then use localhost:5000 to access the API. Here is a great tutorial I've used on setting up Flask on Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04.

Python, Flask, 'Hello World': No browser reaction

I have pip-installed Flask and HTML5 on my Window-system. When I start the Hello World!-program with IDLE, I get a red message in the Python-Shell:
"* Running on xxxx://127.0.0.1:5000/". (xxxx = http)
And when I start it with app.run(debug=True) another red message appears:
"* Restarting with reloader".
My browser (Firefox) shows no reaction.
What can I do to get 'Hello World' in a new tab of Firefox?
The Code is:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
return and app.run are indended
You have to open a new tab with this url:
http://127.0.0.1:5000/
You need to actually open the page in your browser - it won't open itself. Open Firefox and navigate to
127.0.0.1:5000
(it's a URL)
When you run your code, it sits around waiting for a request from the user. When it gets a request, it'll return a response, and that's (sort of) what you see in your browser. Going to a URL is how you send that request - Flask will interpret anything sent to 127.0.0.1:5000 as a request, and try to match the URL to one of your #app.route decorators. For example, if you were to have a function decorated with #app.route("/hello"), then when you go to 127.0.0.1:5000/hello, Flask would run that function to determine the response.
Try out this code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
refrence Flask at first run: Do not use the development server in a production environment
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Try this, this works for me. Open your firefox browser and go to the address given in the output. ex: http://XXXX.X.X.X:5000/

Deploying Flask app to Heroku - "Connection in Use"

I'm deploying a simple Flask app to Heroku - (first time using Flask and Heroku both). When I try to deploy, I get an "Application Error" and the page tells me to try again in a few minutes. The logs state - the "connection [is] in use", retries a few times and then the worker exits (I can post the logs if that is helpful).
My demo.py file:
import flask, flask.views
import os
import urllib2
from bs4 import BeautifulSoup
opener = urllib2.build_opener()
app = flask.Flask(__name__)
app.secret_key = "bacon"
class View(flask.views.MethodView):
def get(self):
return flask.render_template('index.html')
def post(self):
url = (flask.request.form['url'])
ourUrl = opener.open(url).read()
soup = BeautifulSoup(ourUrl)
title = soup.title.text
recipe = soup.find("div", {"id": "recipe"}).getText()
flask.flash(title)
flask.flash(recipe)
return self.get()
app.add_url_rule('/', view_func=View.as_view('main'), methods=['GET', 'POST'])
app.debug = True
app.run()
My procfile is:
web: gunicorn demo:app
If I change the procfile to web: python demo.py, I am able to run the app locally using Foreman but still cannot deploy to Heroku.
Any help is very much appreciated. This is my first time doing this!!
Thank you.
I figured it out. Need to add the following before app.run()
if __name__ == "__main__":
Now it runs fine on Heroku.

Categories

Resources