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

时间:2019-07-17 08:00:14

标签: python urllib2

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

Example <- function ()
{
    data = data.frame (
            content     = c ("point1", "point2", "point3"),
            start       = c ("2010-03-28", "2012-01-17", "2013-12-15"),
            end         = c ("2010-03-28", "2012-01-17", "2013-12-15"),
            type        = c ("point", "point", "point"),
            style       = c ("color: red;", "color: blue;", "color: red;"),
            className   = c ("red_point", "blue_point", "red_point"))

    ui <- fluidPage(
        title = "Rami is testing styles",
        tags$head(
            tags$style(HTML("
                        .red_point  { border-color: red;    }
                        .blue_point { border-color: blue;   }
                        "))),
        timevisOutput("timeline")
    )

    server <- function (input, output, session) {
        output$timeline <- renderTimevis ({
            timevis (data = data, options = list(stack = FALSE))
        })
    }
    shinyApp(ui = ui, server = server)
}

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

我对Python非常陌生,也许我缺少明显的东西。请告知。

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

1 个答案:

答案 0 :(得分:0)

显然,问题在于depositUrl的类型是unicode而不是str。因此,urllib2.Request()方法期望所有参数的类型为unicode。当我进行以下转换时,一切都正常工作:

depositUrl = str(depositUrl)
相关问题