UnicodeDecodeError issue

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Aug 31 07:28:52 EDT 2013


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)

The second line is too late, the error has already occurred.


-- 
Steven



More information about the Python-list mailing list