【发布时间】:2017-04-04 06:34:06
【问题描述】:
我的 PyCurl 代码的目的是遍历 IP 地址数组,然后打印每个 IP 地址的响应正文。
唯一的问题是,一些IP实际上是离线的,当PyCurl无法连接到IP时,它会出错并退出脚本。
我希望脚本做的是,如果 PyCurl 无法连接到 IP,则跳到下一个。这应该很简单,但我不确定我应该如何重写我的代码来允许这个异常。
这是我的代码:
for x in range (0, 161):
url = 'http://' + ips[x] + ':8080/version/'
storage = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, storage.write)
#c.perform errors and once it errors, I would like to increment x to skip to the next IP.
c.perform()
c.close()
content = storage.getvalue()
print content
如上,c.perform 出错,一旦出错,我想增加 x 以跳到下一个 IP。
我怎样才能做到这一点?
提前谢谢你。
【问题讨论】:
标签: python python-2.7 for-loop curl pycurl