【发布时间】:2015-09-02 13:40:30
【问题描述】:
我对“BaseHTTPServer.BaseHTTPRequestHandler”响应的 HTML 中显示的文本有疑问。 HTML 页面接收带有 utf-8 字符的字符串值并将其显示在文本框中,但 utf-8 字符是可见的。
该值保存在xml文件中,格式如下:[15-09-02 10:16:45] Testing%2Bthe%2Bcomments%2Bpage,由python脚本读取并在页面中调用,url如下:
htt://URL/Comments?group=BLABLA&unit=YUO&info=[15-09-02 10:16:45] Testing%2Bthe%2Bcomments%2Bpage&another=
但是,html 页面在文本框中显示以下文本:
[15-09-02 10:16:45] Testing%2Bthe%2Bcomments%2Bpage
没有删除特殊字符,我在字符串中尝试了 encode().decode() 并且没有任何效果。有人有什么想法吗? 用于创建网络服务器的代码:
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
if("/Comments" in s.path):
strServer = "http://" + HOST_NAME + ":" + str(PORT_NUMBER) + "/SaveComments";
strUrl = s.path;
s.wfile.write(CommentsPage.IndexPage(strUrl, strServer));
elif("/SaveComments" in s.path):
s.wfile.write(CommentsPage.SaveComments(s.path));
else:
s.wfile.write(CommentsPage.ErrorPage());
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
返回html页面的函数:
def IndexPage(strUrl, strServer):
...
strPage = "<!DOCTYPE html><html>";
strPage = strPage + "<head><title>Match report comments.</title></head>";
strPage = strPage + "<body><form action=\"" + strServer + "\">";
strPage = strPage + "Group:<br><input type=\"text\" name=\"group\" value=\"" + arrGroupValue[1] + "\">";
strPage = strPage + "<br>Unit:<br><input type=\"text\" name=\"unit\" value=\"" + strUnit + "\">";
strPage = strPage + "<br>Information:<br><textarea rows=\"8\" cols=\"30\" name=\"info\">" + strInfo + "</textarea>";
strPage = strPage + "<br>Resp:<br><input type=\"text\" name=\"responsible\" value=\"" + strResp + "\">";
strPage = strPage + "<br><br><input type=\"submit\" value=\"Submit\"></form></body></html>";
return strPage;
【问题讨论】:
标签: python html encoding utf-8