2016-11-27 87 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! 

參考文獻: