Search code examples
pythonwgetzabbix

Which python library can perform a complex `wget` operation?


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.


Solution

  • 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: