一、问题
[Sun Oct 02 16:37:55 2016] [error] [client 127.0.0.1] File "D:/software/Apache_HTTP_Server2.2/cgi-bin/hello_test_get.py", line 23\r
[Sun Oct 02 16:37:55 2016] [error] [client 127.0.0.1] SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb2 in position 0: invalid start byte\r
在Apache http sever 中用CGI编程 一直报以上错误
py代码如下:
#! D:/software/anaconda3/python.exe
#-*- coding:UTF-8 -*-
#Created on 2016年10月2日
#@author: c
# CGI处理模块
import cgi, cgitb
# 创建 FieldStorage 的实例化
form = cgi.FieldStorage()
# 获取数据
site_name = form.getvalue('name')
site_url = form.getvalue('url')
print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset='gb2312'>")
print ("<title>菜鸟教程 CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2>%s官网:%s</h2>" % (site_name, site_url))
print ("</body>")
print ("</html>")
以上代码在Pydev中跑没有问题
基本知识:在python中默认的编码格式是 utf-8。所以怎么会报不能按 utf-8来解码嘞?一头雾水啊。
问题的解决:
使用notepad++打开
hello_test_get.py
发现文件存储的格式是ANSI
只要将保存文件的格式换成UTF-8就好了
只用notepad++打开test.py >> 菜单栏Encoding(编码)>> Convert to UTF-8(转化成utf-8)
在运行test.py问题解决