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

Dave Angel davea at davea.name
Thu Jul 4 06:29:25 EDT 2013


On 07/04/2013 06:03 AM, Νίκος wrote:
> Στις 4/7/2013 12:59 μμ, ο/η Dave Angel έγραψε:
>> On 07/04/2013 04:37 AM, Νίκος wrote:
>>> I just started to have this error without changing nothing
>>>
>>> in my index.html(template) and metrites.py(which ipen the template)
>>>
>>> [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97] Original
>>> exception was: [Thu Jul 04 11:35:14 2013] [error] [client
>>> 108.162.229.97] Traceback (most recent call last): [Thu Jul 04
>>> 11:35:14 2013] [error] [client 108.162.229.97]   File
>>> "/home/nikos/public_html/cgi-bin/metrites.py", line 19, in <module>
>>> [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97]     host =
>>>  socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
>>> 'UnResolved' [Thu Jul 04 11:35:14 2013] [error] [client
>>> 108.162.229.97] UnicodeDecodeError: 'utf-8' codec can't decode byte
>>> 0xb6 in position 0: invalid start byte [Thu Jul 04 11:35:14 2013]
>>> [error] [client 108.162.229.97] Premature end of script headers:
>>> metrites.py
>>>
>>>
>>> Why cant it decode the starting byte? what starting byte is that?
>>
>> The error message means that somebody is trying to decode a byte string
>> into Unicode, and using the utf-8 codec for it.  Only certain sequences
>> are legal in utf-8, and the first byte of a character may not be 0xb6.
>> So it gives an error.  The question is where does this string come from.
>>
>>
>> Well, the message shows the source line from metrites.py:
>>
>> host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
>> 'UnResolved'
>>
>> So the most likely candidate is the string in the environment named
>> "REMOTE_ADDR"  Can you display that string?  it should look like
>>
>> "11.24.32.4"
>>
>> or some other valid IP address.
>>
>>
>> I'm assuming Python 2.7.  You should specify the python version when
>> starting a new thread, as we (or at least I) cannot keep track of what
>> version everyone's running.
>
> Ima using Python v3.3.2 Dave
>
> The host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
> 'UnResolved' should not have give an error since i explicityl tell it
> that if it cannot resolve dns the ip to hostname to set it as "unresolved"

That's not true.  The 'or' doesn't get executed until after the 
gethostbyaddr() call has returned.  So if something's wrong with that 
call, and it throws an exception, the 'or "unresolved"' won't help.

I don't know that this is the problem, and I'm not all familiar with 
these api's.  But I cannot see anything else that could go wrong there 
to give that particular exception.  Unless the hostname it's going to 
return is a byte string.

>
> The error appear ONLY when i CloudFlare superhost.gr
>
> If i pause tthe domain form CloudFlare then my website loads properly.
>
>

I don't really know what CloudFlare is, and have no idea what 'pausing 
the form' will do.  But since it has something to do with dns, perhaps 
it's returning an invalid host name, one that isn't encoded in utf-8.

-- 
DaveA




More information about the Python-list mailing list