UnicodeDecodeError issue

Ferrous Cranus nikos at superhost.gr
Sat Aug 31 08:58:11 EDT 2013


Στις 31/8/2013 2:28 μμ, ο/η Steven D'Aprano έγραψε:
> On Sat, 31 Aug 2013 11:31:13 +0300, Ferrous Cranus wrote:
>
>> Here is the code inside files.py:
>
> The code you show is not the ENTIRE code inside of files.py, is it? You
> are only showing us a small piece, correct?
>
> The code you show:
>
>> try:
>> 	gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
>> 	city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
>> 	  gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
>> 	host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or
>> 	  socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or
>> 	  os.environ['REMOTE_ADDR']
>> except Exception as e:
>> 	print( repr(e), file=open( '/tmp/err.out', 'w' ) )
>
>
> does not contain a call to cur.execute. And here is your error:
>
>
>> [error] [client 108.162.229.116] Traceback (most recent call last):,
>> referer: http://superhost.gr/ [Sat Aug 31 08:29:33 2013] [error] [client
>> 108.162.229.116]   File "/home/nikos/public_html/cgi-bin/files.py", line
>> 135, in <module>, referer: http://superhost.gr/
>> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116]
>> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
>> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer:
>> http://superhost.gr/
>> [Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] NameError:
>> name 'host' is not defined, referer: http://superhost.gr/
>
>
> Extracting out the critical part:
>
> line 135
> cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
> (%s, %s, %s, %s)''', (filename, host, city, lastvisit) ),
>
> Where is the call to cur.execute in the code snippet you show above?
>
>
>
>> But 'host' defaults to an ip address if it cannot resolve the hostname.
>> Why the errro says its undefined?
>
> Because it is is undefined. Python is not lying to you. If Python tells
> you there is an error, BELIEVE IT. Resolving the hostname is irrelevant.
>
> print(host)  ### ERROR OCCURS HERE BECAUSE HOST IS UNDEFINED ###
> host = socket.gethostbyaddr(addr)
I'm not saying Python is lying of course it does not.

But how is this possible since:


host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] ) or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] ) or 
os.environ['REMOTE_ADDR']


it must have a value by defaulting to something.

The cur.execute fails because it make use of 'host' which is undefined.

		# Try to insert the file into the database
		cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES 
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) )


And the question remain as to why 'host' is undefined.
-- 
Webhost <http://superhost.gr>



More information about the Python-list mailing list