我对HTML中显示的文本有问题“BaseHTTPServer.BaseHTTPRequestHandler“回答。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页面在文本框中显示以下文本:
^{pr2}$在没有删除特殊字符的情况下,我尝试了字符串中的encode().decode(),但没有任何效果。有人知道吗? 用于创建Web服务器的代码:
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;
实际上,解决方案是使用
urllib.unquote
。当url保存在xml文件中时,在必须使用urllib.quote(url)
格式化之前,以及在第一次从文件中读取它时,必须使用urllib.unquote(read_url)
进行格式化。然而,空格被“+”符号代替,函数字符串。替换()解决了这个问题。谢谢你的帮助!在这和UTF-8没有任何关系。这不是任何类型的字符集“编码”,只是URL转义。在
您可以通过使用
urllib.parse.unquote
(python3)或urllib.unquote
(python2)来取消数据的转义。在相关问题 更多 >
编程相关推荐