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

Python - bulk set up pycurl options from dictionary

请告诉我如何从字典中批量设置 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)

提前致谢!

你可以直接使用Python的getattr方法。开头和你的一样,然后

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