访问嵌入式网页出错:The requested URL '/' resolves to a file which is marked executable but is not a CGI file

本文介绍了一个关于thttpd服务中403 Forbidden错误的问题及解决过程。错误原因是网页文件被错误地赋予了可执行权限,通过调整权限设置解决了该问题。

问题:

在目标板中开启了thttpd服务;

在/home/httpd/html/目录下放置包括index.html在内的所有网页;

在/home/httpd/html/cgi-bin/目录下放置pdu.cgi文件,pdu.cgi的权限为777;

在/etc/thttpd.con中的内容是:

# This section overrides defaults
dir=/home/httpd/html
# chroot
nochroot
user=admin# default = nobody
logfile=/var/log/thttpd.log
pidfile=/var/run/thttpd.pid
# This section _documents_ defaults in effect
port=80
# nosymlink# default = !chroot
# novhost
# nocgipat
cgipat=/cgi-bin/*
# nothrottles
# host=0.0.0.0
# charset=iso-8859-1

从web页面访问PDU:192.168.1.244,出现如下错误提示:

错误:403 forbidden.
The requested URL '/' resolves to a file which is marked executable but is not a CGI file;

解决办法:

1. 原来从未碰到这样的错误提示,放狗搜了一下,说有可能是网页文件的权限具有可执行权限导致的;

到/home/httpd/html/目录下看了一下,真是很有可能,因为原来不具有可执行权限的网页文件*.html,现在居然全都是755的权限;

将网页文件的权限去掉:

# chmod a-x *.html
再次在浏览器中访问192.168.1.244,可以顺利访问了。


问题解决。


仔细想了想,原来网页界面交由新来的小伙儿做过更改,然后,在他给我的文档中所有的网页文件都被加上了可执行权限。

但并没有引起我的注意,所以,才会出现上述访问错误。





你遇到的错误: > **The method is not allowed for the requested URL.** 说明你尝试用 `GET` 方法访问了一个只接受 `POST` 方法的路由。 在我们上面的代码中,`/sudoku` 路由只接受 `POST` 请求,因为它是由首页的难度选择表单提交跳转过来的。如果你直接在浏览器地址栏输入 `http://127.0.0.1:5000/sudoku`,浏览器会默认使用 `GET` 方法访问,就会报这个错误。 --- ## ✅ 解决方案 我们需要修改 `/sudoku` 路由,让它既能处理 `GET` 请求(直接访问时跳转回首页),也能处理 `POST` 请求(难度选择后跳转来)。 --- ### ✅ 修改后的 `/sudoku` 路由代码如下: ```python @app.route('/sudoku', methods=['GET', 'POST']) def show_sudoku(): if request.method == 'GET': # 如果用户直接访问 /sudoku,则跳转回首页 return """ <script>window.location.href = "/";</script> <p>正在跳转,请稍等...</p> """ difficulty = request.form.get('difficulty', 'easy') full_board, puzzle = generate_puzzle(difficulty) session['full_board'] = full_board session['difficulty'] = difficulty css = """ <style> body { font-family: "Comic Sans MS", cursive, sans-serif; background: linear-gradient(to right, #c3cfe2, #c3cfe2); text-align: center; padding: 20px; } table { margin: 0 auto; border-collapse: collapse; width: 80vmin; height: 80vmin; } td { border: 1px solid #333; width: 11.1%; height: 11.1%; text-align: center; vertical-align: middle; font-size: 2em; transition: transform 0.2s; } input { width: 100%; height: 100%; text-align: center; font-size: 1.5em; border: none; outline: none; } input:focus { transform: scale(1.2); background-color: #ffe0b2; } .btn { padding: 10px 20px; margin: 10px; font-size: 1em; border-radius: 8px; border: none; cursor: pointer; background-color: #8ec5fc; color: white; transition: 0.3s; } .btn:hover { background-color: #4e74e5; } </style> """ html = f""" <html> <head><title>数独游戏 - {difficulty.capitalize()}</title>{css}</head> <body> <h1>数独游戏 - {difficulty.capitalize()}</h1> <form id="sudoku-form" action="/check" method="post"> <table> """ for i in range(9): html += "<tr>" for j in range(9): cell_value = str(puzzle[i][j]) if puzzle[i][j] != 0 else "" readonly = "readonly" if puzzle[i][j] != 0 else "" html += f""" <td> <input type="text" name="cell_{i}_{j}" value="{cell_value}" maxlength="1" {readonly}> </td> """ html += "</tr>" html += """ </table> <button class="btn" type="button" onclick="provideHint()">提示</button> <button class="btn" type="submit">提交答案</button> </form> <script> function provideHint() { fetch('/hint') .then(res => res.json()) .then(data => { const cell = document.querySelector(`[name="cell_${data.i}_${j}"]`); if (cell.value === "") { cell.value = data.value; cell.readOnly = true; cell.style.backgroundColor = "#d1ffd1"; } }); } </script> </body> </html> """ return render_template_string(html) ``` --- ## ✅ 同时,你还需要修改 `/hint` 路由,因为它目前没有处理 `GET` 请求: ```python @app.route('/hint', methods=['GET', 'POST']) def hint(): full_board = session.get('full_board') if not full_board: return {"error": "no board"}, 400 for i in range(9): for j in range(9): name = f'cell_{i}_{j}' val = request.form.get(name, '') if val == "": return {"i": i, "j": j, "value": full_board[i][j]} return {"error": "no empty cell"}, 400 ``` --- ## ✅ 最后,确保你运行的是完整 Flask 应用 运行命令: ```bash python sudoku_app.py ``` 然后访问: ``` http://127.0.0.1:5000/ ``` 不要直接访问 `/sudoku` 页面,而是通过首页选择难度来进入游戏。 --- ## ✅ 总结问题原因和修复点 | 问题 | 原因 | 解决方案 | |------|------|-----------| | `The method is not allowed` | `/sudoku` 只接受 `POST`,但你用了 `GET` | 添加 `GET` 方法支持,或通过首页跳转访问 | | JavaScript 提示功能失败 | `/hint` 没有处理 `GET` 请求 | 修改 `/hint` 支持 `GET` 或使用 `POST` | | 页面风格丢失 | JS 中的 `j` 变量未定义 | 修复 `provideHint()` 函数中的变量名错误 | --- ## ✅ 修复后的 JS 提示功能(请替换原来的 `<script>` 部分) ```html <script> function provideHint() { fetch('/hint') .then(res => res.json()) .then(data => { if (data.error) return; const cell = document.querySelector(`[name="cell_${data.i}_${data.j}"]`); if (cell && cell.value === "") { cell.value = data.value; cell.readOnly = true; cell.style.backgroundColor = "#d1ffd1"; } }); } </script> ``` --- ##
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值