【发布时间】:2021-03-28 01:35:07
【问题描述】:
我正在尝试在树莓派上使用瓶子和 python 3 来基本上将网页加载到 pi 的 IP 地址上。这应该允许我在笔记本电脑上的浏览器中输入 pi 的 IP,并在网页上的单击按钮为我的程序提供输入。但是,当我尝试这个时,我收到一个权限错误,说权限被拒绝
这是代码、网页代码和错误
代码:
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')
网页代码(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>
错误:
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
代码和网页代码在同一个文件夹中。我最初遇到了瓶子的问题,但是后来我将这两个文件移到了里面有瓶子的文件夹中,然后当我运行它时,它从上面给出了错误。
我尝试将 home.tpl 作为 home.html。当我在浏览器中打开 home.html 时,它正确显示了网络代码。当我在浏览器中打开 home.tpl 时,它只会给我屏幕上显示的代码。如果我使用 home.tpl 或 home.html 运行 python 文件,则会显示相同的错误(是的,我在代码中将文件从 .tpl 更改为 .html)
有没有人知道什么可以帮助我解决这个问题?任何帮助将不胜感激。
【问题讨论】:
-
除非您以 root 身份运行,否则您无法侦听端口 80。您可以尝试使用
sudo运行,也可以尝试绑定到1024 以上的另一个端口 -
@khuynh 所以我将脚本运行为 sudo python filename.py
-
不,不要以
sudo运行它。而是将其更改为使用port=8000并将您的浏览器指向那里。 -
@ronrothman 我用 sudo 运行它并且它有效,我有什么理由应该使用 port=8000
-
sudo这样就打开了一个安全漏洞。 (也没有必要。)
标签: python raspberry-pi bottle