gpt4 book ai didi

python - PyCurl 类型错误 : invalid argument to setopt

转载 作者:行者123 更新时间:2023-12-05 07:49:27 25 4
gpt4 key购买 nike

我正在使用 PyCurl 发送带有文件附件的 POST 请求:

d = pycurl.Curl()
d.setopt(pycurl.URL, url)
# d.setopt(pycurl.RETURNTRANSFER, True)
d.setopt(pycurl.POST, True)
d.setopt(pycurl.POSTFIELDS, {filename: "@" + filename})
b = StringIO.StringIO()
d.setopt(pycurl.WRITEFUNCTION, b.write)
d.perform()
d.close()
message = b.getvalue()

我得到:

Something went wrong, invalid arguments to setopt
Traceback (most recent call last):
File "hasoff.py", line 214, in create_offers_for_advertiser
if filename:
TypeError: invalid arguments to setopt

怎么了?

最佳答案

pycurl.POSTFIELDS 需要 URL 编码的数据。 From docs :

post_data = {'field': 'value'}
postfields = urlencode(post_data)
c.setopt(c.POSTFIELDS, postfields)

您不能使用 pycurl.POSTFIELDS 发送文件。

可以使用 pycurl.HTTPPOST 发送文件。 From docs :

c.setopt(c.HTTPPOST, [
('fileupload', (
c.FORM_FILE, __file__,
c.FORM_FILENAME, 'helloworld.py',
c.FORM_CONTENTTYPE, 'application/x-python',
)),
])

关于python - PyCurl 类型错误 : invalid argument to setopt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com