Skip to main content
 首页 » 编程设计

python之在 Python 中跑道上传文件失败

2024年10月25日9bjzhanghao

我正在尝试上传到跑道但失败了。 以下摘录:

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') 

这总是导致以下错误。

Traceback (most recent call last): 
  File "C:\Python34\libs\podio-py-master\attach_invoices.py", line 43, in <module> 
    filep = c.transport.POST(url='/file/v2/',body=attributes,type='multipart/form-data') 
  File "C:\Python34\libs\podio-py-master\pypodio2\transport.py", line 135, in __call__ 
    body = "".join(body) 
  File "C:\Python34\libs\podio-py-master\pypodio2\encode.py", line 376, in __next__ 
    return next(self) 
  File "C:\Python34\libs\podio-py-master\pypodio2\encode.py", line 352, in __next__ 
    block = next(self.param_iter) 
  File "C:\Python34\libs\podio-py-master\pypodio2\encode.py", line 245, in iter_encode 
    block = self.encode(boundary) 
  File "C:\Python34\libs\podio-py-master\pypodio2\encode.py", line 233, in encode 
    if re.search("^--%s$" % re.escape(boundary), value, re.M): 
  File "C:\Python34\lib\re.py", line 166, in search 
    return _compile(pattern, flags).search(string) 
TypeError: can't use a string pattern on a bytes-like object 

我知道它必须用字节编码等做一些事情,但我不知道如何处理它。即使我尝试将该源设为文件、原始文件或其他任何内容,POST 也会失败。

请您参考如下方法:

这对我有用:

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