Try ISO-8859-1 if unicode fails

master
mutantmonkey 2011-09-22 17:56:28 -04:00
parent 279e8ba969
commit 456efaefe1
1 changed files with 10 additions and 2 deletions

12
web.py
View File

@ -21,7 +21,11 @@ def get(uri):
if not uri.startswith('http'):
return
u = urllib.request.urlopen(uri)
bytes = u.read().decode('utf-8')
bytes = u.read()
try:
bytes = bytes.decode('utf-8')
except UnicodeDecodeError:
bytes = bytes.decode('ISO-8859-1')
u.close()
return bytes
@ -38,7 +42,11 @@ def post(uri, query):
return
data = urllib.parse.urlencode(query)
u = urllib.request.urlopen(uri, data)
bytes = u.read().decode('utf-8')
bytes = u.read()
try:
bytes = bytes.decode('utf-8')
except UnicodeDecodeError:
bytes = bytes.decode('ISO-8859-1')
u.close()
return bytes