2016-03-08 142 views
0

我使用下面的方法獲取的gravatar如何檢測無效的網址與網址抓取

def fetch_gravatar(email): 
    incorrect_base_url = 'http://ww.grvatar.com/avatar/' 
    correct_base_url = 'http://www.gravatar.com/avatar/' 
    gravatar_url = correct_base_url + hashlib.md5(email).hexdigest() + '?' 
    gravatar_url2 = incorrect_base_url + hashlib.md5(email).hexdigest() + '?' 
    size = str(feconf.GRAVATAR_SIZE_PX) 
    gravatar_url += urllib.urlencode({'d':'identicon', 's':size}) 
    result = urlfetch.fetch(gravatar_url, headers={'Content-Type': 'image/png'}) 
    if result.status_code == 200: 
     encoded_body = base64.b64encode(result.content) 
     print result.status_code 
     return 'data:{};base64,{}'.format('image/png', encoded_body) 
    else: 
     print result.status_code 
     return '/images/avatar/user_blue_72px.png' 

無論我是否調用urlfetchgravatar_urlgravatar_url2它送花兒給人打印result.status_code是200如何檢查我網址好還是不好?

回答