我正在使用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
怎么了?
答案 0 :(得分:0)
pycurl.POSTFIELDS
需要网址编码数据。 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',
)),
])