【问题标题】:Which python library can perform a complex `wget` operation? [closed]哪个 python 库可以执行复杂的 `wget` 操作? [关闭]
【发布时间】:2017-04-11 09:46:12
【问题描述】:

我正在连接到 Zabbix,但由于它没有为我想要的资源之一提供 API,我不得不使用 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 来验证和检索图像。

【问题讨论】:

  • requests 可以很好地处理这个问题。你可以使用requests'Session机制来自动处理cookies。
  • 太好了,谢谢。您的评论使我看到了这篇 SO 帖子:stackoverflow.com/questions/15778466/…
  • @ForceBru 感谢您的建议。我现在得到了答案。

标签: python wget zabbix


【解决方案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!

参考资料:

【讨论】:

    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2013-08-05
    • 2010-10-20
    • 2011-03-22
    • 2011-04-04
    • 2014-07-06
    相关资源
    最近更新 更多