无法 post Python 中的 zip 文件。 Unicode解码错误

Cannot post a zip file in Python. Unicode decoding error

当尝试使用 urllib2 提交 zip 文件时,我收到带有以下消息的 UnicodeDecodeError:

Exception during urlopen: 'ascii' codec can't decode byte 0xf1 in position 12: ordinal not in range(128)
Exception: 'ascii' codec can't decode byte 0xf1 in position 12: ordinal not in range(128)
Exception of type: <type 'exceptions.UnicodeDecodeError'>
Exception. Message: "". Doc: "Unicode decoding error.".
Exception during export: 
e.__doc__=Unicode decoding error.

在行 response = urllib2.urlopen(request) 上引发异常。

    def depositZipFile(tempZipFileName, tempZipFilePath, depositUrl, tr):
        print('depositZipFile(). tempZipFileName=%s, tempZipFilePath=%s, depositUrl=%s, tr=%s' % (tempZipFileName, tempZipFilePath, depositUrl, str(tr)))
        with open(tempZipFilePath, 'rb') as f:
            zipData = f.read()
            print('depositZipFile(). type(zipData)=%s' % type(zipData))

            headers = {
                'In-Progress': 'true',
                'Content-Disposition': 'filename=' + tempZipFileName,
                'Content-Type': 'application/zip',
                'Content-Length': os.stat(tempZipFilePath).st_size,
                'Content-Transfer-Encoding': 'binary',
                'Packaging': 'http://purl.org/net/sword/package/METSDSpaceSIP',
            }

            try:
                request = urllib2.Request(depositUrl, data=zipData, headers=headers)

                try:
                    response = urllib2.urlopen(request)
                except Exception as e:
                    print('Exception during urlopen: ' + str(e))
                    raise e

                print('Got response. response=%s' % str(response))

                xmlText = response.read()
                xmlRoot = ET.fromstring(xmlText)
                linkElement = xmlRoot.find('xmlns:link[@rel="alternate"]', namespaces=dict(xmlns='http://www.w3.org/2005/Atom'))

                if linkElement is None:
                    raise ValueError('No redirection URL is found in the response.')

                href = linkElement.attrib['href']
                return href
            except urllib2.HTTPError as e:
                print('HTTPError: ' + str(e))
                print('HTTPError: %s' % str(e.code))
                print('HTTPError message: %s' % e.read())
                raise e
            except Exception as e:
                print('Exception: ' + str(e))
                print('Exception of type: %s' % type(e))
                print('Exception. Message: "%s". Doc: "%s".' % (e.message, e.__doc__))
                raise e

在调用上述方法之前,使用基本身份验证对用户进行身份验证。看下面的方法。

    def authenticateUser(tr, url):
        user = getConfigurationProperty(tr, 'user')
        password = getConfigurationProperty(tr, 'password')
        realm = getConfigurationProperty(tr, 'realm')
        pm = urllib2.HTTPPasswordMgr()
        pm.add_password(realm, url, user, password)
        authHandler = urllib2.HTTPBasicAuthHandler(pm)
        opener = urllib2.build_opener(authHandler)
        urllib2.install_opener(opener)

我是 Python 的新手,也许我遗漏了一些明显的东西。请指教

我正在使用 Python 2.7,Jython 实现。

显然问题是 depositUrl 的类型是 unicode 而不是 str。因此,urllib2.Request() 方法需要所有参数的 unicode 类型。当我进行以下转换时,一切都开始工作了:

depositUrl = str(depositUrl)