Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Python-powered Web Server example question

Fri Nov 13, 2015 9:33 pm

Hello,

I am new to programming and am trying to figure out how to do the Build a python powered web server example that is on the raspberry pi learning example. https://www.raspberrypi.org/learning/py ... ith-flask/ however, I am not getting
[If everything has been written correctly, you should see an output similar to this:

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
instead, I am getting this
pi@raspberrypi ~webapp $ python app.py
* Restarting with stat
* Debugger is active!
* Debugger pin code: 951-758-098
Can someone please tell me what I am doing wrong? I've tried to find other examples on the internet, but none of them are working here is the code i used

Code: Select all

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')

User avatar
DougieLawson
Posts: 39181
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python-powered Web Server example question

Fri Nov 13, 2015 9:51 pm

Change the name of app.py to anything else call it web_app.py. There's probably an existing python library with that name and python is insane in the way it searches the PYTHON_PATH for libraries versus open code.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Fri Nov 13, 2015 10:33 pm

alright I changed it to firsthelloapp but got the same results. By the way The ctrl+s button doesn't seem to save it automatically, i had to save it using the mouse.

Is there anything in the code that I need to use to make it use python 3.4.2 instead of python 2.7.9 which pulls up automatically when I follow the directions as posted on the Raspberry instruction page for this experiment?

User avatar
DougieLawson
Posts: 39181
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python-powered Web Server example question

Fri Nov 13, 2015 11:02 pm

When I run it I get

Code: Select all

pi@eagle:/tmp$ python fl_app.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
192.168.1.30 - - [13/Nov/2015 23:00:53] "GET / HTTP/1.1" 200 -
192.168.1.30 - - [13/Nov/2015 23:00:53] "GET /favicon.ico HTTP/1.1" 404 -
192.168.1.30 - - [13/Nov/2015 23:00:53] "GET /favicon.ico HTTP/1.1" 404 -
192.168.1.30 - - [13/Nov/2015 23:00:53] "GET /favicon.ico HTTP/1.1" 404 -
So there's something broken with your flask installation. I've got 0.10.1 installed on my system.

edit: I just ran sudo pip install -U flask

Now I get

Code: Select all

pi@eagle:/tmp$ python fl_app.py
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 508-617-688
192.168.1.30 - - [13/Nov/2015 23:06:18] "GET / HTTP/1.1" 200 -
^Cpi@eagle:/tmp$
and

Code: Select all

pi@eagle:/tmp$ wget --server-response http://localhost:5000
--2015-11-13 23:08:29--  http://localhost:5000/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:5000... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... 127.0.0.1 - - [13/Nov/2015 23:08:29] "GET / HTTP/1.1" 200 -

  HTTP/1.0 200 OK
  Content-Type: text/html; charset=utf-8
  Content-Length: 11
  Server: Werkzeug/0.11.2 Python/2.7.9
  Date: Fri, 13 Nov 2015 23:08:29 GMT
Length: 11 [text/html]
Saving to: ‘index.html.1’

index.html.1        100%[=====================>]      11  --.-KB/s   in 0s

2015-11-13 23:08:29 (273 KB/s) - ‘index.html.1’ saved [11/11]

pi@eagle:/tmp$

Code: Select all

pi@eagle:/tmp$ cat index.html
Hello worldpi@eagle:/tmp$
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sat Nov 14, 2015 12:11 am

edit: I just ran sudo pip install -U flask
Ha-ha, small world. When you said something was wrong with my flask, I did the same as instructed on flask.pocoo.org/docs/0.10/upgrading/# I couldn't figure out how to log in as root, so I had to use sudo su though.

Now I get the follow errors which are different than the errors I was getting
* Debugger is active!
* Debugger pin code: 339-275-331
I don't know what to do.... I'm glad i'm not the only one with the problem though! Makes me feel better about this, you wouldn't believe how many hours I spent on youtube and other sites trying to figure out how to get a simple server to work on python lol. Any ideas on how to fix this? How do you copy straight from the Linux version of command prompt by the way? ctrl+c doesn't work, nor does the mouse right click provide an option.

User avatar
DougieLawson
Posts: 39181
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Python-powered Web Server example question

Sat Nov 14, 2015 12:29 am

Try pointing a browser at http://pi.local:5000 or http://localhost:5000 or http://192.168.3.14:5000 (or whatever your IP address is).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Python-powered Web Server example question

Sat Nov 14, 2015 1:05 am

Thomas499 wrote: you wouldn't believe how many hours I spent on youtube and other sites trying to figure out how to get a simple server to work on python lol.
Does the built in one work?: python -m SimpleHTTPServer 8000

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sat Nov 14, 2015 2:04 pm

Try pointing a browser at http://pi.local:5000 or http://localhost:5000 or http://192.168.3.14:5000 (or whatever your IP address is).
Do I do this within the code, or are you saying to use the pi and see if I can connect to a browser to see if it has internet connection? If I do this within the code would I change the 0.0.0.0 to the ip address?
Does the built in one work?: python -m SimpleHTTPServer 8000
when I type this in it says
Serving HTTP on 0.0.0.0 port 8000...
does that mean it is working? On arduino, it will tell you which web address to type in to see the server. 0.0.0.0 doesn't sound right, but the example did say that's what you should see.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sat Nov 14, 2015 5:42 pm

I had two other people use my pi, and follow the example they provided. We all had the same results of that stupid error message.

Is there a way to make a simple server using python code without using Flask which may be the problem so I can debug why it isn't working?

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python-powered Web Server example question

Sat Nov 14, 2015 8:40 pm

Thomas499 wrote:I had two other people use my pi, and follow the example they provided. We all had the same results of that stupid error message.

Is there a way to make a simple server using python code without using Flask which may be the problem so I can debug why it isn't working?
This means it works, you can type in your browser YourIP:8000

Otherwise you can try this library: https://github.com/dddomodossola/remi

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sat Nov 14, 2015 9:37 pm

[Traceback (most recent call last):
File "server_pi.py", line 7, in <module>
app.run(host='0.0.0.0', port=80, debug=True)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 671, in run_simple
s.bind((hostname, port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 13] Permission denied
]
This is the message that I keep receiving. Any ideas on how to fix the situation?

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Python-powered Web Server example question

Sat Nov 14, 2015 9:45 pm

Thomas, the problem seems to be a file permision issue. This means that maybe you are running the program with sudo OR you installed Flask with sudo. Is it?

User avatar
croston
Posts: 707
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Python-powered Web Server example question

Sat Nov 14, 2015 9:47 pm

You have to have root privs to run on a port < 1024. I suggest you change the port to something like 8080.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sat Nov 14, 2015 11:17 pm

Thomas, the problem seems to be a file permision issue. This means that maybe you are running the program with sudo OR you installed Flask with sudo. Is it?
Yes, I couldn't figure out how to get into root. When I tried the password raspberry it wouldn't let me in so I had to upgrade flask with sudo as mentioned earlier. If I reinstall Flask using root instead of sudo would that make it work?
Ha-ha, small world. When you said something was wrong with my flask, I did the same as instructed on flask.pocoo.org/docs/0.10/upgrading/# I couldn't figure out how to log in as root, so I had to use sudo su though.
You have to have root privs to run on a port < 1024. I suggest you change the port to something like 8080.
But I need port 110 to run the code (currently working on arduino) to check my email. I don't know if a port over >1024 will let me do that.... Is it a bad idea to run in root for any reason?

I did try using port 8000 and port 900 but I kept getting the errors mentioned in the first post on this thread. I couldn't find a reference on what they meant, and even calling the number like a phone number didn't work.

Should I wipe the sd card and re-install everything from scratch?

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Python-powered Web Server example question

Sat Nov 14, 2015 11:32 pm

Thomas499 wrote:...Yes, I couldn't figure out how to get into root. When I tried the password raspberry it wouldn't let me in so I had to upgrade flask with sudo as mentioned earlier...
By default on Raspbian the root user has no password set, which means you can't log in as the root user from a login prompt. The sudo command allows you to run commands as root. So running a command while logged in as root or running it using sudo has the same effect. There is a page in the Raspberry Pi documentation on Root User / Sudo.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sun Nov 15, 2015 4:10 pm

So running a command while logged in as root or running it using sudo has the same effect.
So loading flask with sudo wasn't the problem then.... I need suggestions on how to get the web server to work, I don't know what the errors I listed on the first post mean, but I do know I had two other people follow the step by step instructions listed on the raspberry example and we all got the same results. Do I need to somehow use python3 instead of the python2 which pops up automatically when following the instructions? Do I need to wipe the sd card and reboot everything from scratch?

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sun Nov 15, 2015 4:46 pm

I'm trying something new as no one can seem to answer the question why the example on the raspberry pi learning page doesn't work or how to fix it.
Here is the attempt to make a server using python.

Code: Select all

import socket

HOST = "192.168.0.177"
PORT = 8000

s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.connect((HOST, PORT))

def move(servo, angle):
	'''Moves the specified servo to the supplied angle.
	
	Arguments:
	servo
	the servo number to command, an integraer from 1-4
	angle
	the desired servo angle, an interger from 0 to 180
	
	(e.g.) >>> servo move(2,90)
	... # "move servo #2 to 90 degrees"'''
	if (0 <= angle <= 180):
		s.send(chr(255))
		s.send(chr(servo))
		s.send(chr(angle))
	else:
		print "Servo angle must be an integer between 0 and 180. \n"
These are the errors I don't know what they mean. The code is only 30 lines or so, why does it say error on line 224? and how do I fix this?
Traceback (most recent call last):
File "ServoServer.py", line 7, in <module>
s.connect((HOST, PORT))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 113] No route to host


------------------
(program exited with code: 1)
Press return to continue





User avatar
rpdom
Posts: 17194
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Python-powered Web Server example question

Sun Nov 15, 2015 6:17 pm

Thomas499 wrote:These are the errors I don't know what they mean. The code is only 30 lines or so, why does it say error on line 224? and how do I fix this?
It says

Code: Select all

Traceback (most recent call last):
File "ServoServer.py", line 7, in <module>
s.connect((HOST, PORT))
first, which is showing the line in your code that is generating the error. Then it goes on to say

Code: Select all

File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 113] No route to host
which is saying that the error actually happened at line 224 of /usr/lib/python2.7/socket.py, which is the file used when you say "import socket".

It looks to me like this code is trying to connect to a servo server on port 8000 at 192.168.0.177, but can't get to that network or IP address. Have you got a server running at that address?

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sun Nov 15, 2015 7:25 pm

It looks to me like this code is trying to connect to a servo server on port 8000 at 192.168.0.177, but can't get to that network or IP address. Have you got a server running at that address?
No, that is the network IP address I use for my arduino server, but it isn't plugged up or running right now.

I am using Remote Desktop connection to control the pi from the lap top so I don't have to buy a keyboard. Does that matter? Do I need to use the pi's ip address instead maybe?

User avatar
rpdom
Posts: 17194
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Python-powered Web Server example question

Sun Nov 15, 2015 7:29 pm

I think you're trying the wrong piece of code. That looks like a client that connects to the server, not the server itself.

Have you got a link to the page where you got it from?


User avatar
rpdom
Posts: 17194
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Python-powered Web Server example question

Sun Nov 15, 2015 8:26 pm

The title of that page is clear "Client (python) to server (arduino)".

The client runs on the Pi. The server runs on the Arduino. The error message is saying "I can't connect to the server on the Arduino", which obviously it can't if it is unplugged.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sun Nov 15, 2015 8:36 pm

Can you show me how to use python as a server then? When I tried the official Raspberry pi example, i got the errors listed on post 1 which no one was able to address.

Thomas499
Posts: 60
Joined: Sun Oct 25, 2015 1:52 pm

Re: Python-powered Web Server example question

Sun Nov 15, 2015 11:16 pm

Ok so I wiped the sd card and started the pi from scratch. see below for the steps and the error codes.
I really need the server to work. i'm new to programming and I don't have a clue why the raspberry pi examples aren't working. Please help!
my code

Code: Select all

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hellow World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8001, debug=True)
Raspberry pi response when try to run it

Code: Select all

pi@raspberrypi ~/webapp $ python app.py
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 951-758-098
This is the error that shows up when I try to run the program in idle.

Code: Select all

Traceback (most recent call last):
  File "/home/pi/webapp/app.py", line 9, in <module>
    app.run(host='0.0.0.0', port=8001, debug=True)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 671, in run_simple
    s.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
>>> 

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Python-powered Web Server example question

Sun Nov 15, 2015 11:54 pm

That seems OK. You're getting that error because the first instance of the program is already running.
There are 10 types of people: those who understand binary and those who don't.

Return to “Python”