에 차단하려면에 전달되지 않습니다.은 왜 변수 값은 마지막으로 아마도 내가 제대로 try..except..finally 블록을 사용하지 않는 파이썬 2.7.10 에 대한 파이썬
웹 페이지에서 수신하는 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 코드로 작동하는 경우 :
got a 200 http response. everything seems good this is the code we got: 200
이것은 우리가 예외를 얻는 경우에 얻을 출력
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 분이 걸립니다). 예외가 발생하면
,'다른!' – mhawke
뭔가 제대로 보이지 않습니다 :''경우 RESP = 200 대신에 충분하다. 오류는'except' 블록에있는 4 개의'print' 명령문 중 첫 번째 출력 만 표시합니다. – chepner