Skip to main content
 首页 » 编程设计

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

2024年12月31日10zhwl

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