为什么我的变量值不能传递给python中的finally块 [英] Why doesn't my variable value get passed to the finally block in python

查看:1109
本文介绍了为什么我的变量值不能传递给python中的finally块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为python 2.7.10
也许我没有使用try..except..finally正确地阻止。

This is for python 2.7.10 Perhaps I'm not using the try..except..finally block correctly.

我需要检查从网页获得的HTTP响应代码。
如果我得到一个200代码,一切正常。如果我得到任何其他代码,请报告什么代码。

I need to check on the HTTP response code I get from a webpage. If I get a 200 code, everything is working. If I get any other code, report what code.

如果我获得了200个HTTP代码,它可以正常工作。
如果我收到异常,由于某种原因,它给了我一个UnboundedLocalError,说明我的变量没有被引用。如何让我的变量在finally块中被识别?

It works fine if I get a 200 HTTP code. If I get an exception, for some reason, it gives me an UnboundedLocalError, stating my variable isn't referenced. How do I get my variable to be recognized in the finally block?

这是我的代码:

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代码一起使用:

This is the output we get if it works with a http 200 code:

got a 200 http response.  everything seems good this is the code we got: 200

这是我们得到的输出例外

This is the output we get if it gets an exception

got some exception
Traceback (most recent call last):
  File "monitorAlive.py", line 27, in <module>
    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分钟,并打了一个问题的网站,那么我得到响应代码/正确的输出。这里可能有第二个问题,为什么网站花费了很长时间才能返回http 500代码(5分钟工作)。

If I wait like 5 min, and hit the website with a problem, then I get the response code / proper output. There might be a second question here as to why the website is taking so long to return the http 500 code (5 min to work).

推荐答案

如果发生异常,则赋值语句( resp = conn.getresponse()。status )永远不会运行或永远不会完成。 1 在这种情况下,当 finally 子句运行时,您会收到错误,因为 resp 从未设置为任何东西。

If an exception happens, then the assignment statement (resp = conn.getresponse().status) either never runs or never finishes.1 In that case, when the finally clause runs, you'll get an error because resp was never set to anything.

根据使用情况,如果您想使用 else 而不是终于终于将运行,无论什么,但 else 只会运行,如果尝试 suite complete没有例外。

Based on the usage, it looks to me like you want to use else instead of finally. finally will run no matter what, but else will only run if the try suite finished without an exception.

1 考虑在 conn中发生的异常。 getresponse - 由于引发异常, conn.getresponse 永不返回任何东西,所以没有值被绑定到左边的 resp

1Consider an exception that happens in conn.getresponse -- Since an exception was raised, conn.getresponse never returns anything so there is no value to be bound to the resp on the left hand side.

这篇关于为什么我的变量值不能传递给python中的finally块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆