python合法的变量名finally_为什么我的变量值在python中不传递给finally块

这是针对python2.7.10的

也许我没有使用try…except…finally block correct。在

我需要检查我从网页上得到的HTTP响应代码。

如果我得到200码,一切正常。如果我有其他代码,报告什么代码。在

如果我得到一个200个HTTP代码就可以了。

如果我得到一个异常,由于某种原因,它会给我一个UnboundedLocalError,声明我的变量没有被引用。如何在finally块中识别变量?在

我的代码是:try:

conn = httplib.HTTPConnection(host, port=9000)

conn.request("GET", "/alive")

resp = conn.getresponse().status

except Exception as e:

print "got some exception"

print "exception " + e

print "Exception msg: " + e.message

print "Args for exception: " + e.args

finally:

if resp == 200:

print "got a 200 http response. everything seems good"

if resp != 200:

print "didn't get a 200 http response. something wrong"

print "this is the code we got: " + str(resp)

这是我们得到的输出,如果它与http 200代码一起工作:

^{pr2}$

这是我们在异常情况下得到的输出got some exception

Traceback (most recent call last):

File "monitorAlive.py", line 27, in

main()

File "monitorAlive.py", line 24, in main

get_status_code(host)

File "monitorAlive.py", line 16, in get_status_code

if resp == 200:

UnboundLocalError: local variable 'resp' referenced before assignment

编辑:如果我等了5分钟,发现网站有问题,我就会得到响应代码/正确的输出。这里可能还有第二个问题,为什么网站要花这么长时间返回http500代码(5分钟才能工作)。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值