我的代码如下所示,但是当它运行时会抛出一个错误。
search_request = urllib2.Request(url,data=tmp_file_name,headers={'X-Requested-With':'WoMenShi888XMLHttpRequestWin'})
#print search_request.get_method()
search_response = urllib2.urlopen(search_request)
html_data = search_response.read()错误是:
Traceback (most recent call last):
File "xx_tmp.py", line 83, in <module>
print hello_lfi()
File "xx_tmp.py", line 69, in hello_lfi
search_response = urllib2.urlopen(search_request)
File "D:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "D:\Python27\lib\urllib2.py", line 406, in open
response = meth(req, response)
File "D:\Python27\lib\urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "D:\Python27\lib\urllib2.py", line 444, in error
return self._call_chain(*args)
File "D:\Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "D:\Python27\lib\urllib2.py", line 527, in http_error_defau
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error我不知道该怎么修?我的意思是,当错误发生时,我的代码如何继续工作?
当我尝试使用
try:
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
pass新错误
UnboundLocalError: local variable 'search_response' referenced before assignment我使用
global search_response并且有错误
NameError: global name 'search_response' is not defined发布于 2012-12-24 19:38:32
你可以捕获异常,这将防止你的程序如此“突然”地停止:
try:
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
print 'There was an error with the request'如果您想继续,您可以简单地:
try:
search_response = urllib2.urlopen(search_request)
except urllib2.HTTPError:
pass这将允许您的程序继续运行;但是您的另一条语句html_data = search_response.read()不会给出预期的结果。要永久地解决这个问题,您需要调试您的请求以了解其失败的原因;这并不是Python所特有的。
发布于 2013-11-27 10:17:56
当我试图向我的GAE Python服务器发送一个大型post请求时,我也遇到了同样的错误。结果是服务器抛出了这个错误,因为我试图将收到的POST字符串写入db.StringProperty()。我将其更改为db.TextProperty(),它不再抛出错误。
来源:Overcome appengine 500 byte string limit in python? consider text
https://stackoverflow.com/questions/14020784
复制相似问题