Search code examples
pythonraspberry-pibottle

Problem with permissions - PermissionError [Errno 13] Permission denied


I am trying to use bottle and python 3 on a raspberry pi to basically load a webpage onto the pi's IP address. This should allow me to type the pi's IP into my browser on my laptop and the click buttons on the webpage to provide inputs into my program. However, when I try this, I get a permission error saying permission denied

Here is the code, the webcode and the error

Code:

from bottle import route, run, template, request
import time

IP_ADDRESS = '192.168.1.8' #Change this to the IP of your Pi

#Handler for the home page
@route('/')
def index():
    cmd = request.GET.get('command', '')
    if cmd == 'f':
        print('Forward' )
    elif cmd == 'l':
        print('Left' )
    elif cmd == 's':
        print('Stop' )
    elif cmd == 'r':
        print('Right' )
    elif cmd == 'b':
        print('Reverse' )
    return template('home.tpl')

#Start the webserver running on port 80
try:
    run(host=IP_ADDRESS, port=80)
finally:
    print('done')

Webcode (home.tpl):

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>

<style>
.controls {
    width: 150px;
    font-size: 32pt;
    text-align: center;
    padding: 15px;
    background-color: green;
    color: white;
}
</style>

<script>
function sendCommand(command)
{
    $.get('/', {command: command});
}
function keyPress(event){
    code = event.keyCode;
    if (code == 119) {
        sendCommand('f');
    }
    else if (code == 97) {
        sendCommand('l');
    }
    else if (code == 115) {
        sendCommand('s');
    }
    else if (code == 100) {
        sendCommand('r');
    }
    else if (code == 122) {
        sendCommand('b');
    }
}
$(document).keypress(keyPress);
</script>
</head>
<body>
<h1>Web Rover</h1>
<table align="center">
<tr><td></td><td class="controls" onClick="sendCommand('f');">W</td><td></td></tr>
<tr><td  class="controls" onClick="sendCommand('l');">A</td>
    <td  class="controls" onClick="sendCommand('s');">S</td>
    <td  class="controls" onClick="sendCommand('r');">D</td>
</tr>
<tr><td></td><td  class="controls" onClick="sendCommand('b');">Z</td><td></td></tr>
</table>
</body>
</html>

Error:

Bottle v0.12.19 server starting up (using WSGIRefServer())...
Listening on http://192.168.1.8:80/
Hit Ctrl-C to quit.

done
Traceback (most recent call last):
  File "/home/pi/Desktop/bottle-0.12.19/Web Test.py", line 24, in <module>
    run(host=IP_ADDRESS, port=80)
  File "/home/pi/Desktop/bottle-0.12.19/bottle.py", line 3137, in run
    server.run(app)
  File "/home/pi/Desktop/bottle-0.12.19/bottle.py", line 2789, in run
    srv = make_server(self.host, self.port, app, server_cls, handler_cls)
  File "/usr/lib/python3.7/wsgiref/simple_server.py", line 153, in make_server
    server = server_class((host, port), handler_class)
  File "/usr/lib/python3.7/socketserver.py", line 452, in __init__
    self.server_bind()
  File "/usr/lib/python3.7/wsgiref/simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)
  File "/usr/lib/python3.7/http/server.py", line 137, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.7/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
PermissionError: [Errno 13] Permission denied

The code and the webcode are in the same folder. I originally had trouble with bottle, but then I moved the two files into the folder with bottle in it, and then when I ran it it gave the error from above.

I tried putting the home.tpl as home.html. When I open home.html in the browser, it displayed the webcode properly. When I open home.tpl in the browser, it just gives me the code displayed on the screen. If I ran the python file with either home.tpl or home.html the same error was displayed (yes I changed the file from .tpl to .html in the code)

Does anyone know anything that could help me with this?? Any help would be greatly appreciated.


Solution

  • You cannot use any port below 1024 (in your case 80) without admin rights. Either use a port greater than 1024 (recommended) or run the script with elevated (sudo) privileges.