2
我正在使用我发布文件的API。但是,当我收到响应时,HTTP状态代码是202.这是可以预料的,但此外API还会响应XML内容。响应代码202,不是合格的错误代码
所以在我的尝试/除了块urllib2.urlopen将导致提出urllib2.HTTPError和销毁XML内容。
try:
response = urllib2.urlopen(req)
except urllib2.HTTPError, http_e:
if http_e.code == 202:
print 'accepted!'
pass
print response.read() # UnboundLocalError: local variable 'response' referenced before assignment
我怎么能期待202和保持响应内容,但不会引发错误?
纠正我,如果我错了,但我不能使用HTTPConnection的urllib2.Request类。 – 2009-12-11 19:41:12
你确实是对的。您正在使用的请求类中是否有特定的东西? – 2009-12-11 19:47:04
是,字节字符串,或者更准确地说是一个zip文件。 – 2009-12-11 20:14:31