无法在Python中发布zip文件。Unicode解码

2024-06-16 16:21:31 发布

您现在位置:Python中文网/ 问答频道 /正文

当尝试使用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非常陌生,也许我遗漏了一些明显的东西。请告知。你知道吗

我使用的是Python2.7,Jython实现。你知道吗


Tags: inresponseastypeexceptioncontenturllib2tr
1条回答
网友
1楼 · 发布于 2024-06-16 16:21:31

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

depositUrl = str(depositUrl)

相关问题 更多 >