如何修复 python3 cgi 和 apache2 中的 UnicodeDecodeError

How to fix UnicodeDecodeError in python3 cgi and apache2

每次我访问我的 apache2 和 python3 CGI 网页时,它都会抛出这个错误: UnicodeDecodeError:'ascii' 编解码器无法解码位置 0 中的字节 0xef:序号不在范围内(128)

我已经尝试重新安装 apache2 和 python3 并在文件打开语句中指定编码。

这是index.py中的代码:

#! /usr/bin/python3
import cgitb
import cgi
import socket
import codecs
cgitb.enable()
print("Content-Type: text/html")
print("")
form = cgi.FieldStorage()
if "code" not in form:
    print(open("main.html","r+").read())

和main.html中的代码:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>gAImeMaster</title>
    <link rel="stylesheet" href="/static/bootstrap-4.3.1-dist/css/bootstrap.min.css" crossorigin="anonymous">
    <style>
        .full-width {
            width:100%;
        }
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
            width: 100%;
        }

        #topnav {
            margin: 0;
            padding: 0.01%;
            padding-left: 1%;
            display: inline-block;
        }

            #topnav h1 {
                display: inline-block;
            }

        a {
            text-decoration: none;
            color: Highlight;
        }
        /* Style the header with a grey background and some padding */
        .header {
            overflow: hidden;
            background-color: #f1f1f1;
            padding: 20px 10px;
        }

            /* Style the header links */
            .header a {
                float: left;
                color: black;
                text-align: center;
                padding: 12px;
                text-decoration: none;
                font-size: 18px;
                line-height: 25px;
                border-radius: 4px;
            }

                /* Style the logo link (notice that we set the same value of line-height and font-size to prevent the header to increase when the font gets bigger */
                .header a.logo {
                    font-size: 25px;
                    font-weight: bold;
                }

                /* Change the background color on mouse-over */
                .header a:hover {
                    background-color: #ddd;
                    color: black;
                }

                /* Style the active/current link*/
                .header a.active {
                    background-color: dodgerblue;
                    color: white;
                }

        /* Float the link section to the right */
        #header-right {
            float: right;
            text-align:right;
        }

        /* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
        @media screen and (max-width: 500px) {
            .header a {
                float: none;
                display: block;
                text-align: left;
            }

            .header-right {
                float: none;
            }
        }
    </style>
    <script src="static/jquery.js"></script>
    <script src="static/js.cookie-2.2.0.min.js"></script>
    <script src="static/bootstrap-4.3.1-dist/js/bootstrap.js"></script>
</head>
<body>
    <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light bg-light header">
            <a class="navbar-brand logo" href="/">gAImeMaster</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="full-width collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="header-right navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="/">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link notlogin" href="/login.html">Login</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link login" href="/logout.py">Logout</a>
                    </li>
                    <!--
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
            <a class="dropdown-item" href="#">Action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#">Something else here</a>
        </div>
    </li>
    <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
    </li> -->
                </ul>
                <!--
            <form class="form-inline my-2 my-lg-0">
                <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
                -->
            </div>
        </nav>
        <!--
        <div class="header">
            <a href="#default" class="logo">gAImeMaster</a>
            <div class="header-right">
                <a class="active" href="/">Home</a>
                <a class="notlogin" href="/login.html">Login</a>
                <a class="login" href="/logout.py">Logout</a>
            </div>
        </div>
            -->
        <hr />
        <footer class="page-footer font-small blue pt-4 fixed-bottom">
            <!-- Copyright -->
            <div class="footer-copyright text-center py-3">
                Coppyright © 2019
                <a href="/pepperworx"> Pepperworx</a>
            </div>
            <!-- Copyright -->
        </footer>
    </div>
    <script>
        if (typeof Cookies.get("login") != "undefined") {
            $(".notlogin").hide();
            $(".login").show();
        } else {
            $(".notlogin").show();
            $(".login").hide();
        }
    </script>
</body>
</html>

HTML 可能是 utf-8,但我的假设是文件本身不是。您是否尝试过 open() 中不是 utf-8 的其他编码?我以前被 'latin-1' 绊倒过。

尝试

print("Content-Type: text/html;charset=utf-8")

仅供参考,如果脚本旨在通过命令行 运行,您还应该声明

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')