python - 在 Python 2.7 "The character encoding of the plain text document was not declared. "中获取错误

标签 python python-2.7 tomcat cgi

我是新手,正在使用 Apache Tomcat 服务器和 Python 2.7 CGI 脚本。

出于测试目的,我正在编写一些代码,但出现以下错误:

The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature

网络.py

import cgi, cgitb   
# Create instance of FieldStorage  
form = cgi.FieldStorage()   
# Get data from fields 
first_name = form.getvalue('first_name') 
last_name  = form.getvalue('last_name')  
print "Content-Type: text/html; charset=utf-8\n\n"
print "<html>" 
print "<head>"
# print "<meta content='text/html;charset=utf-8' http-equiv='Content-Type'>"
# print "<meta content='utf-8' http-equiv='encoding'> "
print "<title>Hello - Second CGI Program</title>" 
print "</head>" 
print "<body>" 
print "<h2>Hello %s %s</h2>" % (first_name, last_name) "
print "</body>" 
print "</html>" 

我的 HTML 表单:

<form action="/cgi-bin/webs.py" method="post"> 
First Name: <input type="text" name="first_name">  <br />  
Last Name: <input type="text" name="last_name" /> 
<input type="submit" value="Submit" /> 
</form>

最佳答案

你应该放一个合适的 meta http-equiv="Content-Type"进入您的 HTML 文档(包含您的表单的页面和由您的 CGI 脚本生成的 HTML)。例如

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

你似乎有一对这样的 <meta>脚本中的标签,但它们都被注释掉了......

请注意,如果您声明 charset是 UTF-8 那么您应该确保文档实际上是 编码为 UTF-8。如果它们仅包含纯 ASCII 安全字符,则您无需执行任何操作,因为这些字符的 UTF-8 版本与其 ASCII 代码相同。

顺便说一句,如果你已经在 Apache 中正确设置了你的路径,你就不需要 /cgi-bin在你的表格中 action属性。


我刚刚测试了您的 Python CGI 脚本和包含表单的正确 HTML 文件。

index.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CGI test</title>
</head>

<body>
<form action="cgi-bin/qwebs.py" method="post"> 
First Name: <input type="text" name="first_name">  <br>  
Last Name: <input type="text" name="last_name"> 
<input type="submit" value="Submit"> 
</form>
</body>
</html>

cgi-bin/qwebs.py

#! /usr/bin/env python

import cgi, cgitb  

# Create instance of FieldStorage  
form = cgi.FieldStorage()

# Get data from fields 
first_name = form.getvalue('first_name') 
last_name  = form.getvalue('last_name')

print "Content-Type: text/html; charset=utf-8\r\n\r\n"
print "<html>" 
print "<head>"
print "<meta content='text/html;charset=utf-8' http-equiv='Content-Type'>"

print "<title>Hello - Second CGI Program</title>" 
print "</head>" 
print "<body>" 
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>" 
print "</html>" 

我使用了 Python 的简单 Web 服务器:

python -m CGIHTTPServer

默认情况下服务于端口 8000,我将 CGI 脚本放在 cgi-bin 中当前目录下的文件夹。

一切正常。所以我怀疑您需要修复 Apache 配置。确保您的 CGI 路径正确并且您已启用 .py被称为 CGI 的文件。

关于python - 在 Python 2.7 "The character encoding of the plain text document was not declared. "中获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34627233/

相关文章:

java - 使用 Tomcat 部署 JAX-RS 应用程序,但始终找不到所需的资源

python - 在 IOError 上打开不同的文件

python - bin/python3 : cannot execute binary file: Exec format error

python - 两个 DataFrame 的复杂合并

python - readline 的构建轮失败 - 在 OSX 上安装 Rekall 时

python - Django - 管理模型中的多重多元化

docker - 内部 Tomcat REST 从 Docker 容器内部调用本地主机

tomcat - 无法找到 Tomcat 5.5.27 的 httpd.conf

python - 导入错误: bad magic number in 'time' : b'\x03\xf3\r\n' in Django

python - 使用 read_csv 转换 StringIO 时使用 Pandas 的奇怪输出