0
我在GAE上的特定URL上使用urllib2.urlopen時遇到問題。當我使用Eclipse運行相同的代碼時,我能夠檢索網站數據,但是當我嘗試使用GAE實現時,出現'狀態500內部服務器錯誤'。urllib2.urlopen()在GAE上返回特定URL上的錯誤500
在普通的Python應用程序中,我有以下代碼可以正常工作。
query2 = {'ORIGIN': 'LOS','DESTINATION':'ABV', 'DAY':'23',
'MONTHYEAR': 'JAN2012', 'RDAY': '-1', 'RMONTHYER': '-1',
'ADULTS': '1', 'KIDS': '0', 'INFANTS': '0', 'CURRENCY': 'NGN',
'DIRECTION': 'SEARCH', 'AGENT': '111210135256.41.138.183.192.29025'}
encoded = urllib.urlencode(query2)
url3 = 'http://www.flyaero.com/cgi-bin/airkiosk/I7/171015'
request = urllib2.urlopen(url3, encoded)
print 'RESPONSE:', request
print 'URL :', request.geturl()
headers = request.info()
print 'DATE :', headers['date']
print 'HEADERS :'
print '---------'
print headers
data = request.read()
print 'LENGTH :', len(data)
print 'DATA :'
print '---------'
print data
這工作得很好,但與GAE,它沒有。這是GAE代碼:
class MainPage(webapp.RequestHandler):
def get(self):
query = {'ORIGIN': 'LOS','DESTINATION':'ABV', 'DAY':'23',
'MONTHYEAR': 'JAN2012', 'RDAY': '-1', 'RMONTHYER': '-1',
'ADULTS': '1', 'KIDS': '0', 'INFANTS': '0', 'CURRENCY': 'NGN',
'DIRECTION': 'SEARCH', 'AGENT': '111210135256.41.138.183.192.29025'}
urlkey = 'http://www.flyaero.com/cgi-bin/airkiosk/I7/181002i?AJ=2&LANG=EN'
urlsearch = 'http://www.flyaero.com/cgi-bin/airkiosk/I7/171015'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = { 'User-Agent' : user_agent }
try:
request = urllib2.urlopen(urlkey)
data = request.read()
info = request.info()
except urllib2.URLError, e:
print 'error code: ', e
print 'INFO:'
print info
print ''
print 'Old key is: ' + query['AGENT']
print 'Agent key is ' + query['AGENT']
encoded = urllib.urlencode(query)
print 'encoded data', encoded
print ''
print 'web data'
print''
try:
request2 = urllib2.urlopen(urlsearch, encoded)
data2 = request2.read()
info2 = request2.info()
except urllib2.URLError, e:
print 'error code: ', e
print 'INFO:'
print info2
print ''
print 'DATA: '
print data
有兩個調用urllib2.urlopen。第一個工作,但第二個返回錯誤500,try-except塊不會捕獲它。
這是由request.info()命令
Status: 500 Internal Server Error
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 1662
我不是develooper服務器上,即時用eclipse顯影,並從本地主機我的系統上運行的打印出該消息。的是,在布勞爾和蝕控制檯上顯示,以及該錯誤消息,這是消息:
WARNING 2011-12-10 17:29:31,703 urlfetch_stub.py:405] Stripped prohibited headers from URLFetch request: ['Host']
WARNING 2011-12-10 17:29:33,075 urlfetch_stub.py:405] Stripped prohibited headers from URLFetch request: ['Content-Length', 'Host']
ERROR 2011-12-10 17:29:38,305 __init__.py:463] ApplicationError: 2 timed out
<pre>Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 700, in __call__
handler.get(*groups)
File "C:\Users\TIOLUWA\Documents\CODES\Elipse\FlightShop\flightshop.py", line 124, in get
request2 = urllib2.urlopen(urlsearch, encoded)
File "C:\python25\lib\urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "C:\python25\lib\urllib2.py", line 381, in open
response = self._open(req, data)
File "C:\python25\lib\urllib2.py", line 399, in _open
'_open', req)
File "C:\python25\lib\urllib2.py", line 360, in _call_chain
result = func(*args)
File "C:\python25\lib\urllib2.py", line 1107, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\python25\lib\urllib2.py", line 1080, in do_open
r = h.getresponse()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\dist\httplib.py", line 213, in getresponse
self._allow_truncated, self._follow_redirects)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 260, in fetch
return rpc.get_result()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 358, in _get_fetch_result
raise DownloadError(str(err))
DownloadError: ApplicationError: 2 timed out
那麼,它可能不是一個URLError然後。但我們當然不知道,因爲你沒有告訴我們錯誤是什麼。它將在日誌中,或者在開發服務器上的控制檯中。 –
@DanielRoseman我添加了從request.info()apprently獲得的錯誤消息,我也從eclipse的控制檯添加了錯誤消息,因爲我沒有在GAE服務器上運行 – TiOLUWA