【问题标题】:BaseHTTPServer.BaseHTTPRequestHandler issue with utf-8utf-8 的 BaseHTTPServer.BaseHTTPRequestHandler 问题
【发布时间】: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


    【解决方案1】:

    这与 UTF-8 无关。这不是任何一种字符集“编码”,而只是 URL 转义。

    您可以使用 urllib.parse.unquote (Python 3) 或 urllib.unquote (Python 2) 取消转义数据。

    【讨论】:

    • 嗨丹尼尔,我在收到网址时插入了以下代码:strurlReceived = urllib.unquote(s.path);,但不起作用,发生了同样的问题。
    【解决方案2】:

    真的,解决方案是urllib.unquote。当url保存在xml文件中时,必须先格式化为urllib.quote(url),第一次从文件中读取必须格式化为urllib.unquote(read_url)。但是空格被“+”符号替换,函数 string.replace() 解决了这个问题。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-12
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      相关资源
      最近更新 更多