Python中Podio文件上载失败

2024-04-26 12:47:21 发布

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

我试图上传到podio,但失败了。 以下摘录:

c = api.OAuthClient(
    podio_pw.client_id,
    podio_pw.client_secret,
    podio_pw.username,
    podio_pw.password,  
source = "dit is een test"
    attributes = {
            'filename' : 'test.txt',
            'source' : source
            }
    filep = 

c.transport.POST(url='/file/v2/',body=attributes,type='multipart/form-data')

这将导致以下错误。在

^{pr2}$

我知道它和字节编码有关,但我不知道如何处理它。即使我试图将源文件变成一个文件、原始文件或其他文件,POST也会失败。在


Tags: 文件testclientapiidsourcesecretusername
3条回答

这对我很有效:

c = api.OAuthClient(
    client_id,
    client_secret,
    username,
    password,    
)

filename = 'screenie.png'
filedata = open(filename, 'r')

"""Create a file from raw data"""
attributes = {'filename': filename,
              'source': filedata}

file_upload = c.transport.POST(url='/file/v2/', body=attributes, type='multipart/form-data')
print(file_upload)

我从这里取了代码:https://github.com/podio/podio-py/blob/master/pypodio2/areas.py

Returns“只能加入iterable错误”

attributes={'filename': 'mx.txt', 'source': 'hello uyur92wyhfr    ruptgpwyoer8t9u'}
    try:
        item = c.transport.POST(url=url,
                                body=attributes,
                                type='multipart/form-data')
    except Exception as e:
        print(e)

要在python3.*中执行文件上载过程,必须更新pypodio中的两个文件。在

第1步

用下面的脚本替换文件encode.py。在

import urllib.request
import http.client
import mimetypes
import codecs
import uuid
import binascii
import io
import os
import sys

def multipart_encode(fields, files):
    content_type, body = MultipartFormdataEncoder().encode(fields, files)
    return body, content_type


class MultipartFormdataEncoder(object):
    def __init__(self):
        self.boundary = uuid.uuid4().hex
        self.content_type = 'multipart/form-data; boundary={}'.format(self.boundary)

    @classmethod
    def u(cls, s):
        if sys.hexversion < 0x03000000 and isinstance(s, str):
            s = s.decode('utf-8')
        if sys.hexversion >= 0x03000000 and isinstance(s, bytes):
            s = s.decode('utf-8')
        return s

    def iter(self, fields, files):
        """
        fields is a sequence of (name, value) elements for regular form fields.
        files is a sequence of (name, filename, file-type) elements for data to be uploaded as files
        Yield body's chunk as bytes
        """
        encoder = codecs.getencoder('utf-8')
        for (key, value) in fields:
            key = self.u(key)
            yield encoder(' {}\r\n'.format(self.boundary))
            yield encoder(self.u('Content-Disposition: form-data; name="{}"\r\n').format(key))
            yield encoder('\r\n')
            if isinstance(value, int) or isinstance(value, float):
                value = str(value)
            yield encoder(self.u(value))
            yield encoder('\r\n')
        for (key, filename, fpath) in files:
            key = self.u(key)
            filename = self.u(filename)
            yield encoder(' {}\r\n'.format(self.boundary))
            yield encoder(self.u('Content-Disposition: form-data; name="{}"; filename="{}"\r\n').format(key, filename))
            yield encoder(
                'Content-Type: {}\r\n'.format(mimetypes.guess_type(filename)[0] or 'application/octet-stream'))
            yield encoder('\r\n')
            with open(fpath, 'rb') as fd:
                buff = fd.read()
                yield (buff, len(buff))
            yield encoder('\r\n')
        yield encoder(' {} \r\n'.format(self.boundary))

    def encode(self, fields, files):
        body = io.BytesIO()
        for chunk, chunk_len in self.iter(fields, files):
            body.write(chunk)
        return self.content_type, body.getvalue()

来自here的代码段

第2步

更新transport.py,第186行

^{pr2}$

相关问题 更多 >