I put together a function 开发者_JAVA技巧to link to news items with django as a filter. It works on dev_appserver but on production server it returns None
Can you tell me why it's not working? Should I investigate the except clause of the code where it currently just passes?
def news(n):
url = os.environ.get('HTTP_HOST') if os.environ.get('HTTP_HOST') else os.environ['SERVER_NAME']
tld = url[url.rfind('.'):]
try:
if url == 'localhost:8080':
result = urlfetch.fetch('http://news.google.se/?output=rss')
elif tld != '.com' and tld != '.se' and tld != '.cl' :
result = urlfetch.fetch('http://news.google.com'+tld+'/?output=rss')
else:
result = urlfetch.fetch('http://news.google.com/?output=rss')
if result.status_code == 200:
dom = minidom.parseString(result.content)
item_node = dom.getElementsByTagName("item")
try:
random_1=random.choice(item_node)
rss1_link = random_1.childNodes[1].firstChild.data
rss1_text = random_1.childNodes[0].firstChild.data
return mark_safe('<a href="%s">%s</a>' % (rss1_link, rss1_text))
except IndexError,e:
return ''
except urlfetch.Error, e:
pass
register.filter(news)
Update: Now it returns an empty string on production but locally it works. It's something else that status 200 on production:
def news(n):
url = os.environ.get('HTTP_HOST') if os.environ.get('HTTP_HOST') else os.environ['SERVER_NAME']
tld = url[url.rfind('.'):]
try:
if url == 'localhost:8080':
result = urlfetch.fetch('http://news.google.se/?output=rss')
elif tld != '.com' and tld != '.se' and tld != '.cl' :
result = urlfetch.fetch('http://news.google.com'+tld+'/?output=rss')
else:
result = urlfetch.fetch('http://news.google.com/?output=rss')
if result.status_code == 200:
dom = minidom.parseString(result.content)
item_node = dom.getElementsByTagName("item")
try:
random_1=random.choice(item_node)
rss1_link = random_1.childNodes[1].firstChild.data
rss1_text = random_1.childNodes[0].firstChild.data
return mark_safe('<a href="%s">%s</a>' % (rss1_link, rss1_text))
except IndexError,e:
return ''
else:
return ''
except urlfetch.Error, e:
logging.error(str(e))
return ''
EDIT: Here's the simplest reproduction that return a status 200 locally and a status 503 on production
def status(n):
try:
result = urlfetch.fetch('http://news.google.com/?output=rss')
return str(result.status_code)
except urlfetch.Error, e:
return 'error'
Update: Here's the solution I currently use. It still needs improfement since there is a possibility for choosing 2 news items that are the same:
import random
def updateFeed(url):#to do, get srv from url and find number of entries
srv = os.environ.get('HTTP_HOST') if os.environ.get('HTTP_HOST') else os.environ['SERVER_NAME']
tld = srv[srv.rfind('.'):]
url = 'http://news.google.com/?output=rss'
if srv.endswith('.com.br'):
url = 'http://news.google.com.br/?output=rss'
elif srv == 'localhost:8080' or srv.endswith('alltfunkar.com'):
url = 'http://news.google.se/?output=rss'
elif tld != '.com' and tld != '.se' and tld != '.cl' :
url = 'http://news.google.com'+tld+'/?output=rss'
query_args = { 'q': url, 'v':'1.0', 'num': '15', 'output': 'json' }
qs = urllib.urlencode(query_args)
loader = 'http://ajax.googleapis.com/ajax/services/feed/load'
loadurl = '%s?%s' % (loader, qs)
logging.info(loadurl)
result = urlfetch.fetch(url=loadurl,headers={'Referer': '...'})
if result.status_code == 200:
news = simplejson.loads(result.content)
""" not working, using random.randrange instead
some_key = random.choice(news.keys())
something = news[some_key]
"""
i = random.randrange(0,10)#to do: instead of 10, it should be number of entries
title = news[u'responseData'][u'feed'][u'entries'][i][u'title']
link = news[u'responseData'][u'feed'][u'entries'][i][u'link']
return mark_safe('<a href="%s">%s</a>' % (link, title))
Python functions that don't explicitly return
anything will return None
. If this code is returning None
on your production server, it's probably because it's hitting that last except: pass
block, as you mentioned.
Without reading the actual code (which I haven't), I'd say replace that pass
with return ''
to safely swallow the urlfetch.Error
or decide what you want to happen in that case and implement some new code for that block.
精彩评论