Python - 批量设置字典中的pycurl选项

时间:2018-06-13 17:07:29

标签: python pycurl

请告诉我如何从字典中批量设置pycurl选项。

示例:

options = {
    'WRITEFUNCTION': buffer.write,
    'FOLLOWLOCATION': True,
    'HEADER': True,
    'VERBOSE': False,
    }
options2 = {
    'COOKIEFILE': cookie_file,
    'COOKIEJAR': cookie_file,
    }
options.update(options2)

我需要这样的东西:

for opt, val in options.items():
    curl.setopt(pycurl.opt, val)

提前谢谢你!

1 个答案:

答案 0 :(得分:1)

您可以使用Python的getattr方法。开始与你的相同,然后

for opt, val in options.items():
    curl.setopt(getattr(curl,opt), val)