-1

I'm connecting to Zabbix, but since it doesn't provide an API for one of the resources I want, I had to use wget to get the job done.

Which python library would allow me to perform a "complex" wget operation?

By "complex," I mean:

 # 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'

Not sure if requests would get the job done? I need to 1) login and save the returned cookies, 2) use the returned cookie to authenticate and retrieve the image.

sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • 3
    `requests` can handle this perfectly well. You could use a `requests`' `Session` mechanism to deal with the cookies automatically. – ForceBru Nov 27 '16 at 16:45
  • Great, thanks. Your comment led me to this SO post: http://stackoverflow.com/questions/15778466/using-python-requests-sessions-cookies-and-post – sivabudh Nov 27 '16 at 16:50
  • @ForceBru thanks for your suggestion. I got the answer now. – sivabudh Nov 27 '16 at 17:05

1 Answers1

2

Thank you @ForceBru for your suggestion. I managed to work out the solution now! Here it is:

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!

References:

Community
  • 1
  • 1
sivabudh
  • 31,807
  • 63
  • 162
  • 228