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

Dave Angel davea at davea.name
Fri Jul 5 03:50:52 EDT 2013


On 07/05/2013 02:51 AM, Νίκος Gr33k wrote:

     <SNIP>
>
> Please help because i just happened to noticed that after having this code:
>
> try:
>      host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
> except Exception as e:
>      host = "Reverse DNS Failed"

Don't ever catch a bare Exception class.  Make it more specific to the 
particular problem you're trying to suppress.

In particular, your previous problem with the utf-8 decoding will also 
be caught by the Exception class, so it'll get all lumped together as 
"Reverse DNS Failed".

>
>
> all requests are being resolves, result to:
>
>
> Reverse DNS Failed as you can see here:
> http://superhost.gr/?show=log&page=index.html
>
> How can the above code not be able to reeverse dns any more and it falls
> back to  the failed string?
>

Since you've not made any progress with all the other suggestions, how 
about if you decompose this line into several, and see just which one is 
failing?  Maybe that'll tell you what's going on.  In general, 
suppressing an exception without knowing why it's firing is a huge mistake.

The line started as:

 >      host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

refactor that to:
        remadd = os.environ('REMOVE_ADDR')
        tuple3 = socket.gethostbyaddr(remadd)
        host = tuple3[0]

and see which one throws the exception.  Then once you have that, 
examine the exact parameters that might be triggering the problem.  In 
particular, figure out the exact types and values for remadd and tuple3.

        print(type(remadd) + " : " + repr(remadd))

Of course, print itself won't work too well in a CGI environment.  But 
you must have solved that problem by now, either using log files or 
running the program excerpt in a regular console.


-- 
DaveA




More information about the Python-list mailing list