Python custom 404 response error -


i wrote hiscore checker game play, enter list of usernames .txt file & outputs results in found.txt.

however if page responds 404 throws error instead of returning output " 0 " & continuing list.

example of script,

#!/usr/bin/python  import urllib2  def get_total(username):  try:   req = urllib2.request('http://services.runescape.com/m=hiscore/index_lite.ws?player=' + username)   res = urllib2.urlopen(req).read()   parts = res.split(',')   return parts[1]  except urllib2.httperror, e:   if e.code == 404:    return "0"  except:   return "err"  filename = "check.txt"  accs = [] handler = open(filename) entry in handler.read().split('\n'):  if "no displayname" not in entry:   accs.append(entry) handler.close()  account in accs:  display_name = account.split(':')[len(account.split(':')) - 1]  total = get_total(display_name)  if "err" not in total:   rstr = account + ' - ' + total   handler = open('tried.txt', 'a')   handler.write(rstr + '\n')   handler.close()   if total != "0" , total != "49":    handler = open('found.txt', 'a')    handler.write(rstr + '\n')    handler.close()   print rstr  else:   print "error searching"   accs.append(account)  print "done" 

httperror exception doesn't seem working,

 except urllib2.httperror, e:   if e.code == 404:    return "0"  except:   return "err" 

error response shown below.

python error

now understand error shown doesn't seem related response of 404, occurs users return 404 response request, other request works fine. can assume issue within 404 response exception.

i believe issue may lay in fact 404 custom page redirected too?

so original page " example.com/index.php " 404 " example.com/error.php "?

not sure how fix.

for testing purposes, format use is,

id:user:display

which placed check.txt

it seems total can end being none. in case can't check has 'err' in it. fix crash, try changing line to:

if total not none , "err" not in total: 

to more specific, get_total returning none, means either

  • parts[1] none or
  • except urllib2.httperror, e: executed but e.code not 404.

in latter case none returned exception caught you're dealing specific 404 case , ignoring other cases.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -