程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Apache CGI programming: how to solve the problem of web page garbled code and calling cookie garbled code? AH01215: UnicodeEncodeError: ‘gbk‘ codec can‘t encode char

編輯:Python

1, Directly in html Add in code “charset="text/html;utf-8”.

This method , Will directly let the browser use gbk Coding mode . Although this can solve the problem of web page garbled , But it can't fundamentally solve the coding problem . If you decide not to use some functions containing Chinese later , Then you can use this method , Otherwise, it is not recommended .

Here is an example of , As shown below :

label 1 Location code , Surface browser will adopt gbk The encoding mode of , It's true . After operation, as shown in the figure :

Mark 2、3 Location code , It is set by getting the previous web page cookie. At this time, the browser will make an error when running this script . open apache24 Medium log file , stay erro We can see the error log in , As shown in the figure below :

Through these four red boxes , We found the root cause of the script running error , The last red box has explained :

AH01215: UnicodeEncodeError: 'gbk' codec can't encode character '\\x8f'

This mistake , Let's be frank , There is something wrong with the coding , The web code is gbk code , and python The script uses utf-8 code , So it's using print When printing ,apache Error reporting or direct garbled display .

So in order to avoid these problems , I recommend you to use the second solution to web page coding 、 The method of garbled code .

2,python In file , Indicate how the console is encoded , Add :

import sys

sys.stdout=codecs.getwriter('utf-8')(sys.stdout.buffer);

These two lines of code , Be similar to java Output stream socket method in ,getwriter(“utf-8”) Return to one streamwritter(‘parameter’) function , and sys.stdout.buffer As a parameter , for streamwritter(‘parameter’) Use . This indicates sys.stdout The encoding method of standard output stream is utf.

In this way , The coding method of the whole web page is ’utf’ 了 . As shown in the figure above , Add tag 1 After the code , The code of the whole web page is utf-8 了 , As shown in the following figure :

It solves the problem of the whole web page coding , Finally, we have to solve python Function call cookie The garbled code problem occurs when . Mark as shown in the figure 4 Shown , We need to learn from cookie Code the field value obtained in , Final output utf-8 Content of coding , That is, use this code :

‘your getting str’.encode(‘latin-1’).decode(‘utf-8’)

Here is a point to pay attention to :cookie use latin-1 The encoding mode of , So we're going to use encode take cookie Press latin-1 To decode , Finally, utf-8 Encoded as a string . Final , The script will run correctly ! As shown in the last figure above , This python The document was read correctly cookie The field values of the .

I'm the son of Science , One is learning python New programmers , Aspire to engage in testing 、 Game development 、 big data 、AI Direction ! Pay attention to me , Share with you from time to time python Programming dry goods ! Make a little progress every day , Grow a big step every day !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved