[issue6854] UnicodeDecodeError when retrieving binary data from cgi.FieldStorage()

loveminix report at bugs.python.org
Mon Sep 7 07:13:35 CEST 2009


New submission from loveminix <loveminix at yahoo.com.cn>:

The following cgi applet uploads a binary file to the server. It gets a
"UnicodeDecodeError" inside cgi.FieldStorage(). The same code works in
python 2.6.

#! /usr/bin/python3

import os, cgi;
import cgitb; cgitb.enable();

pathInfo = os.environ.get("PATH_INFO", "");
serverName = os.environ.get("SERVER_NAME", "");
scriptName = os.environ.get("SCRIPT_NAME", "");

if pathInfo == "":
    print(
"""\
Content-type: text/html
"""
);
    print(
"""\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
    <div>
        <form action="http://{0}{1}/upload"
enctype="multipart/form-data" method="post">
        <p>
            <input type="file" name="file"><br>
            <input type="submit" value="Upload">
        </p>
        </form>
    </div>
</body>
</html>\
""".format(serverName, scriptName)
);
elif pathInfo == "/upload":
    fieldStorage = cgi.FieldStorage();
    fileItem = fieldStorage["file"];
    if fileItem.filename != "":
        file = open("/tmp/test.txt", mode="wb");
        file.write(fileItem.file.read());
        file.close();
        print(
"""\
Content-type: text/html
"""
);
        print(
"""\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
    <div>
        <p>Success</p>
    </div>
</body>
</html>\
"""
);

----------
components: Library (Lib)
messages: 92343
nosy: loveminix
severity: normal
status: open
title: UnicodeDecodeError when retrieving binary data from cgi.FieldStorage()
type: behavior
versions: Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6854>
_______________________________________


More information about the Python-bugs-list mailing list