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

Νίκος nikos at superhost.gr
Thu Jul 4 12:36:17 CEST 2013

-------- Αρχικό μήνυμα --------
Θέμα: Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in 
position 0: invalid start byte
Ημερομηνία: Thu, 04 Jul 2013 06:29:25 -0400
Από: Dave Angel <davea at davea.name>
Προς: python-list at python.org
Ομάδες συζήτησης: comp.lang.python
Αναφορές: <kr3c7k$tjs$2 at news.grnet.gr> 
<mailman.4214.1372931978.3114.python-list at python.org> 
<kr3hab$jn0$1 at news.grnet.gr>

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] Original
>>> exception was: [Thu Jul 04 11:35:14 2013] [error] [client
>>>] Traceback (most recent call last): [Thu Jul 04
>>> 11:35:14 2013] [error] [client]   File
>>> "/home/nikos/public_html/cgi-bin/metrites.py", line 19, in <module>
>>> [Thu Jul 04 11:35:14 2013] [error] [client]     host =
>>>  socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
>>> 'UnResolved' [Thu Jul 04 11:35:14 2013] [error] [client
>>>] UnicodeDecodeError: 'utf-8' codec can't decode byte
>>> 0xb6 in position 0: invalid start byte [Thu Jul 04 11:35:14 2013]
>>> [error] [client] 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
>> ""
>> 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.

So how do you propose to write the above statement?
I was under the i impression then if for any reason the gethostbyaddr 
failed then or's argument will be returned instead.

> 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.

I think so too.

What is now proved was at first only imagined!

More information about the Python-list mailing list