Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Flask server is not working

Tue Mar 20, 2018 6:21 am

Hello friends

I am trying to run flask server on PI3 rasbain but no luck it say site can not be reached

Code: Select all

  from flask import Flask

  app = Flask(__name__)

  @app.route('/')
  def index():
      return 'Hello world'

  if __name__ == '__main__':
      app.run(host='0.0.0.0', debug=True, port=8080)
[email protected]:~ $ cd webapp
[email protected]:~/webapp $ python3 app.py
[email protected]:~/webapp $

I am trying with different port number 5000 and 4996 but no luck


[email protected]:~ $ sudo apt-get update
Hit:1 http://mirrordirector.raspbian.org/raspbian stretch InRelease
Hit:2 http://archive.raspberrypi.org/debian stretch InRelease
Reading package lists... Done
[email protected]:~ $ sudo apt-get -y upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
[email protected]:~ $ sudo apt-get -y install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version (9.0.1-2+rpt2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
[email protected]:~ $ sudo pip3 install flask

texy
Forum Moderator
Forum Moderator
Posts: 5151
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Flask server is not working

Tue Mar 20, 2018 7:59 am

Same as this person, but a different issue !!
viewtopic.php?f=91&t=208492

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: Flask server is not working

Tue Mar 20, 2018 9:42 am

texy wrote:
Tue Mar 20, 2018 7:59 am
Same as this person, but a different issue !!
viewtopic.php?f=91&t=208492

Texy
Thanks
how to troubleshoot the problem, I don find any error

charlietango_22
Posts: 28
Joined: Sun Feb 25, 2018 9:34 pm

Re: Flask server is not working

Tue Mar 20, 2018 11:15 am

Fan174 wrote:
Tue Mar 20, 2018 9:42 am
texy wrote:
Tue Mar 20, 2018 7:59 am
Same as this person, but a different issue !!
viewtopic.php?f=91&t=208492

Texy
Thanks
how to troubleshoot the problem, I don find any error
are you trying to connect locally (from the same PI) or remotely from another machine?
I'm asking because your code runs on my PI (PI 3 B, Stretch) without any issue. This is my output when launching the py file:

Code: Select all

[email protected]:~ $ python flasktest.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 263-868-971

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: Flask server is not working

Tue Mar 20, 2018 12:09 pm

charlietango_22 wrote:
Tue Mar 20, 2018 11:15 am
Fan174 wrote:
Tue Mar 20, 2018 9:42 am
texy wrote:
Tue Mar 20, 2018 7:59 am
Same as this person, but a different issue !!
viewtopic.php?f=91&t=208492

Texy
Thanks
how to troubleshoot the problem, I don find any error
are you trying to connect locally (from the same PI) or remotely from another machine?
I'm asking because your code runs on my PI (PI 3 B, Stretch) without any issue. This is my output when launching the py file:

Code: Select all

[email protected]:~ $ python flasktest.py
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 263-868-971

there was some space problem but now server is running on Pi. I was trying to control led but it say url not found

Code: Select all

from flask import Flask, render_template
 
from flask_responses import  json_response, xml_response, auto_response
import RPi.GPIO as GPIO
import datetime

app = Flask(__name__)

@app.route("/toggle", methods=['post'])
def toggle():
 GPIO.setmode(GPIO.BOARD)
 GPIO.setup(7, GPIO.OUT)
 state = not GPIO.input(7)
 GPIO.output(7,state)

 return json_response({"status": state}, status_code=201)

if __name__ =="__main__":
 app.run(host='0.0.0.0', debug=True, port=4000)

* Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 283-476-405
192.168.0.40 - - [20/Mar/2018 11:55:49] "GET / HTTP/1.1" 404 -
192.168.0.40 - - [20/Mar/2018 11:55:49] "GET /favicon.ico HTTP/1.1" 404 -
192.168.0.40 - - [20/Mar/2018 11:55:59] "GET / HTTP/1.1" 404 -

charlietango_22
Posts: 28
Joined: Sun Feb 25, 2018 9:34 pm

Re: Flask server is not working

Tue Mar 20, 2018 12:34 pm

Fan174 wrote:
Tue Mar 20, 2018 12:09 pm

there was some space problem but now server is running on Pi. I was trying to control led but it say url not found

You configured your server to handle http request aimed at [host]:[port]/toggle, if you send an http request to get / , it won't work.

Examples:
192.168.0.40:4000 -> 404 not found
192.168.0.40:4000/toggle -> should work.

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: Flask server is not working

Tue Mar 20, 2018 12:50 pm

charlietango_22 wrote:
Tue Mar 20, 2018 12:34 pm
Fan174 wrote:
Tue Mar 20, 2018 12:09 pm

there was some space problem but now server is running on Pi. I was trying to control led but it say url not found

You configured your server to handle http request aimed at [host]:[port]/toggle, if you send an http request to get / , it won't work.

Examples:
192.168.0.40:4000 -> 404 not found
192.168.0.40:4000/toggle -> should work.
I tried that's not working

charlietango_22
Posts: 28
Joined: Sun Feb 25, 2018 9:34 pm

Re: Flask server is not working

Tue Mar 20, 2018 3:47 pm

what exactly is 'not working'?

SeanElder
Posts: 5
Joined: Tue Mar 20, 2018 7:32 am
Location: London

Re: Flask server is not working

Tue Mar 20, 2018 5:50 pm

HAHAHA hello My friend. I am starting on the raspberry pi curriculum now. you can find it here https://curriculum.raspberrypi.org/ .

Have a look at each creator page and see if its more your level. It is mine.

:mrgreen:

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: Flask server is not working

Wed Mar 21, 2018 7:58 am

charlietango_22 wrote:
Tue Mar 20, 2018 3:47 pm
what exactly is 'not working'?
When I type ip address on browser it say url not found

requested url was not found on the server

I think my code is wrong. I don't know exactly what's the problem

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Flask server is not working

Wed Mar 21, 2018 8:17 am

you must show us
-a- the actual python code
-b- the start line in terminal and the msg on it
-c- the full URL you type in browser
-d- of same RPI or a other computer in your network

_______________________________________________________________
when you start your ( python flask ) server you see like
[email protected]:~/projects/py3_flaskserver $ ./start
* Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 243-692-839
for STOP must press [ctrtl][c] in that terminal window

if you see a different msg at start please tell us.
________________________________________________________________

if you start the browser on this RPI use
http://0.0.0.0:4000/
or
http://localhost:4000/
or
http://127.0.0.1:4000/
( all 3 should work )

on a browser of a computer on your LAN need the IP of this RPI like with
ifconfig
or point with mouse over your WLAN ICON in the HEADER line
tooltip will tell you the IP of this RPI.
like
192.168.1.203/24
so must use in PC browser:
http://192.168.1.203:4000/
_________________________________________________________________
working tip:
in the following i show commands (bold) needed to type into terminal window ( in desktop OR VNC desktop OR PUTTY / SSH)
and also code / content for files...
i expect that you can copy paste from this browser / forum post
first the command lines and execute with [ENTER]
and then the content into the editor window.
_________________________________________________________________
you have a working directory like
/home/pi/projects/py3_flaskserver/
( if not make it and go there )

and inthere a python code: ( edit with)
nano app.py

Code: Select all

#!/usr/bin/python3

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
        return 'Hello world'

if __name__ == '__main__':
        app.run(debug=True, host='0.0.0.0', port=4000)

you should make a bash script to start the thing:
nano start

Code: Select all

#!/bin/bash
python3 /home/pi/projects/py3_flaskserver/app.py
and make it executable
chmod +x start
now you could use
./start
but better
optional / just to make it more easy
nano start_my_server.desktop

Code: Select all

[Desktop Entry]
Type=Application
Name=start my_server
Exec=/home/pi/projects/py3_flaskserver/start
Icon=network
Comment=start python3 flask webserver
Terminal=true
X-KeepTerminal=false
Categories=Network;
and
nano browse_my_server.desktop

Code: Select all

[Desktop Entry]
Type=Link
URL=http://localhost:4000
Name=browse my_server
Icon=leafpad.png
Comment=start browser python3 flask webserver
this 2 .desktop files pls copy to the desktop at /home/pi/Desktop/
cp start_my_server.desktop /home/pi/Desktop/start_my_server.desktop
cp browse_my_server.desktop /home/pi/Desktop/browse_my_server.desktop

________________________________________________________
even i used the "nano" editor here better practice for the python code is
geany app.py
as that can show the blank and TAB indentation what is so essential for python
Image
you see the 2 desktop icons for start server and start browser, also the open GEANY editor window and the optional direct START from there,
as it is not ONLY a editor.

cfo64nc
Posts: 3
Joined: Tue Feb 06, 2018 12:57 pm

Re: Flask server is not working

Tue Nov 27, 2018 7:37 pm

I had the same problem. I was trying to run the flask app using:

$ export FLASK_APP=app.py flask run

when I checked from another terminal session with 'ss -l' I got:

$ ss -l | grep 5000
tcp LISTEN 0 128 127.0.0.1:5000 *:*

Note that the app is only listening on the address 127.0.0.1 (i.e., localhost, local connections only).

I solved it by running the Flask app using python. e.g.

$ python app.py

which resulted in 'ss -l' showing the port being available from all addresses:

$ ss -l | grep 5000
tcp LISTEN 0 128 *:5000 *:*

Note that the app is now listening on all addresses (*.5000).

Hope that helps.

Return to “Beginners”