简体   繁体   中英

Python 2 cgi field storage post variable encoding

I can't get this piece of information anywhere.

Does anyone know what the encoding of a string retrieved via a python cgi fieldstorage object is?

For example, I have this html form:

<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="style.css"/>
<body>
    <form id="mainForm" method="post" action="./uploadArticle.py">
        <input type="text" name="articletitle" /></br>
        <textarea name="articlebody" rows="50" cols="100"/></textarea></br>
    <input type="submit"/>
    </form>
</body>
</html>

That I process through this python file with apache :

import cgi
form = cgi.FieldStorage()
title = form["articletitle"].value
content = form["articlebody"].value

Will title and content be utf8 encoded strings?

The question shows a little bit of confusion about what encoding is.

Data received over the internet is just bytes. Python has no way of knowing what "encoding" it is; that's entirely dependent on how it was produced.

In Python 3, that data will be a bytestring; in Python 2, it will be a string, but neither of these have an intrinsic encoding.

If you do know the source encoding you can decode it to a unicode string; utf-8 is a good guess, and will do the right thing in most circumstances, but it can be wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM