UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

Dave Angel davea at davea.name
Fri Jul 12 17:46:35 CEST 2013


On 07/12/2013 07:56 AM, Ferrous Cranus wrote:
> Στις 12/7/2013 2:47 μμ, ο/η Wayne Werner έγραψε:
>> On Thu, 4 Jul 2013, Νίκος Γκρ33κ wrote:
>>
>>> Στις 4/7/2013 6:10 μμ, ο/η MRAB έγραψε:
>>>> What do you mean "I don't know how to catch the exception with
>>>> OSError"? You've tried "except socket.gaierror" and "except
>>>> socket.herror", well just write "except OSError" instead!
>>>
>>>
>>> try:
>>>     host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
>>> except OSError:
>>>     host = "UnResolved"
>>>
>>> produces also an internal server error.
>>>
>>> Are you sure is just except OSError ?
>>>
>>
>> Have you ensured that 'REMOTE_ADDR' is actually a key in os.environ? I
>> highly recommend using the logging module to help diagnose what the
>> actual exception is.
>>
>> HTH,
>> -W
>
> Yes it is a key, but the problem as i suspected was cloudflare.
> i had to use os.environ['HTTP_CF_CONNECTING_IP'] that cloudflare passes
> as variable i the cgi enviroment in order to retrieve the visitor's ip.
>
>
> try:
>      gi = pygeoip.GeoIP('/usr/local/share/GeoLiteCity.dat')
>      city = gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
>      host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
> except Exception as e:
>      host = repr(e)
>
>
> Sometimes though iam still receiving the usual
> UnicodeDecodeError('utf-8', b'\xc1\xf0\xef\xf4\xf5
>
> but only for a few ip addresses, in moste cases it works.

And naturally, you now know how to debug those UnicodeDecodeError 
problems.  Surely, the code you post here isn't what you actually do, 
because when people spend time to give you detailed advice, you actually 
read it, and work at understanding it.

Chortle, snort.

-- 
DaveA




More information about the Python-list mailing list