当我试图用希腊字母在文本字段中输入数据时,我的wsgi脚本将数据作为jibberish保存在MySQL数据库中,我不知道为什么。 以下是要通过表单方法过账时的相关代码:
pdata = pdata + '''
<form methods="POST" enctype="multipart/form-data" action="%s">
<tr>
<td> <center> <input type="text" name="task" size=50> </td>
<td> <center> <input type="text" name="price" size=5> </td>
<td> <center> <input type="text" name="lastvisit"> </td>
</table><br><br>
<td> <input type="image" src="/static/img/submit.gif" name="update" value="Ενημέρωση!"> </td>
</tr>
</form>
''' % app.get_url( '/update/<name>', name=name )
pdata = pdata + "<meta http-equiv='REFRESH' content='200;%s'>" % app.get_url( '/' )
return pdata
这里是一个相对回调函数,它试图将发布的表单数据输入到MySQL数据库中。在
^{pr2}$我不明白为什么数据以jibberish而不是正确的utf-8存储到数据库中。同样尝试使用utf-8编码类型也没有成功。在
<form methods="POST" enctype="utf-8" action="%s">
根据wsgi_mod文档,WSGIDaemonProcess的默认编码是ASCII。ASCII中不包括希腊字符,并且您的输入未正确解码。如果要允许使用希腊字符,则必须使用UTF-8或iso-8859-1。通常服务器是由init系统启动的deamons,99%的情况下仍然使用ASCII作为默认编码。在开发或调试时,通常不会遇到这些问题,因为python脚本继承了当前用户的环境,而当前用户通常使用UTF-8。在
引用apache的wsgi_mod:
然而,“αΓτήείναιμιαδοκιμή”显然是无效的UTF-8,因为 第38位的字节(
ή
)表示它是一个两字节的UTF-8字符,但后面只有1个字节(reference)。在如果这是传递给代码的数据,那么您需要检查并确认HTML表单以正确的UTF-8格式提交数据。在
假设您的输入字符串是正确的UTF-8编码,那么您的输出字符串“ÎÏÎÎÎÎνÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎ。在
因此,可以将问题(或传输形式)定义为上述的存储格式或HTML格式。在
这也可能是个问题。MySQL
utf8_
是而不是完整的UTF-8(wat?!),因为它是3字节而不是4字节;因此,如果您存储了一个4字节的UTF-8字符,它将偏移所有后面的字符字节,使它们看起来像垃圾。在解决方案:
将MySQL列和所有排序规则更新为
utf8mb4_unicode_ci
还要检查以确保MySQL传输机制也在使用
utf8mb4_
。在还有请Read This
相关问题 更多 >
编程相关推荐