Python CGI

编程入门 行业动态 更新时间:2024-10-25 08:28:14
本文介绍了Python CGI - UTF-8 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 HTML5 和 Python CGI:

For HTML5 and Python CGI:

如果我编写 UTF-8 元标记,我的代码将无法工作.如果我不写,它会起作用.

If I write UTF-8 Meta Tag, my code doesn't work. If I don't write, it works.

页面编码为 UTF-8.

Page encoding is UTF-8.

print("Content-type:text/html")
print()
print("""
    <!doctype html>
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        şöğıçü
    </body>
    </html>
""")

此代码不起作用.

print("Content-type:text/html")
    print()
    print("""
        <!doctype html>
        <html>
        <head></head>
        <body>
            şöğıçü
        </body>
        </html>
    """)

但此代码有效.

推荐答案

来自 https://ru.stackoverflow/a/352838/11350

首先不要忘记在文件中设置编码

First dont forget to set encoding in file

#!/usr/bin/env python
# -*- coding: utf-8 -*-

那就试试

import sys
import codecs

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

或者,如果您使用 apache2,请添加到您的配置中.

Or if you use apache2, add to your conf.

AddDefaultCharset UTF-8    
SetEnv PYTHONIOENCODING utf8

这篇关于Python CGI - UTF-8 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-30 11:22:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/777186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   CGI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!