2016-11-27 71 views
-1

我正在连接到Zabbix,但由于它没有提供我想要的其中一个资源的API,因此我必须使用wget才能完成工作。哪个python库可以执行复杂的`wget`操作?

哪个python库允许我执行“复杂”wget操作?

通过 “情结,” 我的意思是:

# Logging in to Zabbix 
wget -4 --no-check-certificate --save-cookies=z.coo -4 --keep-session-cookies -O - -S --post-data='name=username&password=somepassword&enter=Sign in&autologin=1&request=' 'https://some.zabbix-site.com:50100/index.php?login=1' 

# Grabbing the network image 
wget -4 --no-check-certificate --load-cookies=z.coo -O result.png 'https://some.zabbix-site.com:50100/map.php?sysmapid=5&severity_min=0' 

不知道requests会完成这项工作?我需要1)登录并保存返回的cookie,2)使用返回的cookie来验证和检索图像。

+3

'requests'可以处理这个问题非常清楚的。你可以使用'request'' Session'机制来自动处理cookie。 – ForceBru

+0

太好了,谢谢。您的评论导致我这个SO帖子:http://stackoverflow.com/questions/15778466/using-python-requests-sessions-cookies-and-post – sivabudh

+0

@ForceBru感谢您的建议。我现在得到了答案。 – sivabudh

回答

1

谢谢@ForceBru您的建议。我现在设法解决了这个问题!那就是:

import requests 
s = requests.Session() 
data = {'name': 'username', 'password': 'password', 'enter': 'Sign in', 'autologin': '1', 'request': ''} 
url = 'https://some.zabbix-site.com:50100/index.php?login=1' 
r = s.post(url, data=data) # use this if the SSL certificate is valid 
r = s.post(url, data=data, verify=False) # use this if the SSL certificate is unsigned 
r.cookies # check the cookies value 
re = s.get('https://some.zabbix-site.com:50100/map.php?sysmapid=5&severity_min=0') 
re.content # the file content is here! 

参考文献: