UnicodeDecodeError issue

F

Ferrous Cranus

Suddenly my webiste superhost.gr running my main python script presents
me with this error:

Code:
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')


Does anyone know what this means?
 
C

Chris Angelico

Suddenly my webiste superhost.gr running my main python script presents me
with this error:

Code:
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')


Does anyone know what this means?

Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that
seems unhelpful, it's because your question was; there is not enough
information for us to be able to give any further information.

ChrisA
 
F

Ferrous Cranus

Στις 31/8/2013 9:53 πμ, ο/η Chris Angelico έγÏαψε:
Yes. It means that 0xB6 is an invalid start byte in UTF-8. If that
seems unhelpful, it's because your question was; there is not enough
information for us to be able to give any further information.


Hello Chris,

I provided you with only that info because that only info is displayed
at http://superhost.gr at the top left.

If you please tell me what to try to provide you guys with more info
about this problem.
 
F

Ferrous Cranus

Στις 31/8/2013 10:02 πμ, ο/η Ferrous Cranus έγÏαψε:
Στις 31/8/2013 9:53 πμ, ο/η Chris Angelico έγÏαψε:


Hello Chris,

I provided you with only that info because that only info is displayed
at http://superhost.gr at the top left.

If you please tell me what to try to provide you guys with more info
about this problem.
And the wird thign is that few the last days this error was not there,
just only today it started dissaperaing.
 
P

Peter Otten

Ferrous said:
Suddenly my webiste superhost.gr running my main python script presents
me with this error:
Code:
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')
Does anyone know what this means?
'Unknown hostname'
 
F

Ferrous Cranus

Στις 31/8/2013 10:25 πμ, ο/η Peter Otten έγÏαψε:
'Unknown hostname'
Thanks you decoded in greek-iso, coll thinking!

But llok:

Why can the host varibale be unset since iam using this:


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 'Αγνωστη
ΠÏοέλευση'
except Exception as e:
err = repr(e)

==================
it gets set to string 'Αγνωστη ΠÏοέλευση' if in case visitor's remote ip
address cannot be resolved.

So that error should arise at all.
 
F

Ferrous Cranus

Here is the code inside files.py:

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' ) )


that produces this:


[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] ValueError:
underlying buffer has been detached, referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] , referer:
http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [error] [client 108.162.229.116] Original
exception was:, referer: http://superhost.gr/
[Sat Aug 31 08:29:33 2013] [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/


But 'host' defaults to an ip address if it cannot resolve the hostname.
Why the errro says its undefined?
 
S

Steven D'Aprano

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

Ferrous Cranus

Στις 31/8/2013 2:28 μμ, ο/η Steven D'Aprano έγÏαψε:
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.
 
F

Ferrous Cranus

Στις 31/8/2013 3:58 μμ, ο/η Ferrous Cranus έγÏαψε:
Στις 31/8/2013 2:28 μμ, ο/η Steven D'Aprano έγÏαψε:
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.
Thsi fails too:

host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
"Unkknown host"
 
S

Steven D'Aprano

cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ),
[...]
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']

What's the line number of that line of code? My guess is that it is AFTER
line 135, which is where the error occurs. Or possibly it is inside a
function that hasn't been called. Here is the same failure:

print(host)
host = "this is too late"


Here is the same failure again:

if 0:
host = "this never gets called"
print(host)


Here is the same failure again:

data = []
for x in data:
host = "This never happens"
print(host)


And again, a trickier one this time:

host = "something"
del host # and now it is gone
print(host)


And again:

def func():
global host
print(host)
func()
host = "Too late, the error has already occurred"


One last one:

def set_host():
host = "This is a local variable"
set_host()
print(host)


Study all these examples. Now read your own code. host has not been
defined at the time the cur.execute line is reached. You have to work out
which of my examples best matches your code.

it must have a value by defaulting to something.

No, there is no default value for variables. How long have you been
programming in Python? Six months? A year?

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


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

Because you haven't defined it BEFORE you try to use it. There is no
point defining it AFTER you use it, the error has already occurred.

You have to make the coffee before you drink it:

# This fails too
drink(coffee)
coffee = make_coffee()


# This doesn't help at all
drink(coffee)
coffee = make_coffee() or prepare_coffee()


# This is also useless
drink(coffee)
coffee = make_coffee() or prepare_coffee() or "coffee"


# But this works
coffee = make_coffee()
drink(coffee)
 
F

Ferrous Cranus

Τη Σάββατο, 31 ΑυγοÏστου 2013 9:41:27 Ï€.μ. UTC+3, ο χÏήστης Ferrous Cranus έγÏαψε:
Suddenly my webiste superhost.gr running my main python script presents

me with this error:



Code:

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef

\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,

'invalid start byte')





Does anyone know what this means?

Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of them.
I have study all of your examples and then re-view my code and i can *assure* you that the line statement that tied to set the 'host' variable is veryearly at the top of the script(of course after imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee beforeactually making one first but rather than i cannot drink the coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ
 
C

Chris Angelico

Ye i'm aware that i need to define variables before i try to make use of them.
I have study all of your examples and then re-view my code and i can *assure* you that the line statement that tied to set the 'host' variable is very early at the top of the script(of course after imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee before actually making one first but rather than i cannot drink the coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ

You're setting host inside a try/except block. Are you getting
exceptions that prevent it from being set, perhaps?

Also... do you seriously (a) block access if no Referer: header, and
(b) permit that access anyway if the user has a non-blank cookie named
'admin'? Seriously??

ChrisA
 
F

Ferrous Cranus

Στις 1/9/2013 10:12 πμ, ο/η Chris Angelico έγÏαψε:
You're setting host inside a try/except block. Are you getting
exceptions that prevent it from being set, perhaps?

Also... do you seriously (a) block access if no Referer: header, and
(b) permit that access anyway if the user has a non-blank cookie named
'admin'? Seriously??

ChrisA

I have checked th output of the erro log a tthe very moment that
files.py tris to run and i receive this:


[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] ValueError:
underlying buffer has been detached, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] , referer:
http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] Original
exception was:, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] Traceback
(most recent call last):, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] File
"/home/nikos/public_html/cgi-bin/files.py", line 135, in <module>,
referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113]
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer:
http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] NameError:
name 'host' is not defined, referer: http://superhost.gr/

Let alone that i when i try to set the 'host' variable i get this line
at my '/tmp/err.out'


(e-mail address removed) [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')
 
C

Chris Angelico

Let alone that i when i try to set the 'host' variable i get this line at my
'/tmp/err.out'


(e-mail address removed) [~]# cat /tmp/err.out

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')

So host isn't being set because of this error, which you're blithely
carrying on after and assuming that things have happened. Solve this
problem, only then move on. Otherwise you WILL confuse yourself
further.

ChrisA
 
S

Steven D'Aprano

Τη Σάββατο, 31 ΑυγοÏστου 2013 9:41:27 Ï€.μ. UTC+3, ο χÏήστης Ferrous
Cranus έγÏαψε:

Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of
them. I have study all of your examples and then re-view my code and i
can *assure* you that the line statement that tied to set the 'host'
variable is very early at the top of the script(of course after
imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee
before actually making one first but rather than i cannot drink the
coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ


You are mistaken. In line 20-25, you have this:

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'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
or "Proxy Detected"
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what
the error is? You have access to the err.out file, but apparently you
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of "host"
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and
fix it.
 
D

Dave Angel

Óôéò 1/9/2013 10:12 ðì, ï/ç Chris Angelico Ýãñáøå:

Let alone that i when i try to set the 'host' variable i get this line
at my '/tmp/err.out'


(e-mail address removed) [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')

Let's examine the relevant code from your pastebin above:


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'] )[0] or socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy Detected"
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )

Presumably you do realize that any exception on any of the three lines
will skip the remainder, and print that line to the /tmp/err.out file?
And that if any exception occurs, it'll be before 'host' is initialized
at all? Possibly before 'city' is initialized, or even 'gi' ? So
there's no reason yet to assume that somehow setting the 'host'
variable triggers anything. When in doubt, consider including only one
line in any given try block. But let's try simpler changes.

Your file mode is 'w' which will wipe out anything written earlier. Yet
if the exception does not happen, the file will still contain error(s)
from some earlier run. You should open (and truncate) the file before
the first use, then use the same handle in each exception at which you
want to record any information. Further, you ought to put more into the
file than just the repr(e) value. For starters, the Traceback would be
nice.

If I were doing it, I'd be printing repr(sys.exc_info()) to that file.
I'd also have some kind of label, so that once I had more than one of
these in the code, I'd have an easyt way to tell which was which.

This is my first crack at it (untested):

errout = open("/tmp/err.out", "w") #opens and truncates the error
output file
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'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
"Proxy Detected"
except Exception as e:
print( "Xyzzy exception-", repr(sys.exc_info()), file=errout)
errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier. But right now, you need to stop throwing
away useful information.
 
C

Chris Angelico

Your file mode is 'w' which will wipe out anything written earlier. Yet
if the exception does not happen, the file will still contain error(s)
from some earlier run. You should open (and truncate) the file before
the first use, then use the same handle in each exception at which you
want to record any information.

Hmm, I'd go the other way, and append to the file every time there's
an error - otherwise, one successful page load will wipe out the error
from a previous one. But I agree that the inconsistency is not helping
him.

ChrisA
 
F

Ferrous Cranus

Στις 1/9/2013 1:35 μμ, ο/η Dave Angel έγÏαψε:
This is my first crack at it (untested):

errout = open("/tmp/err.out", "w") #opens and truncates the error
output file
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'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
"Proxy Detected"
except Exception as e:
print( "Xyzzy exception-", repr(sys.exc_info()), file=errout)
errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier. But right now, you need to stop throwing
away useful information.

First of all thank you for your detailed information Dave.
I have tried all you said, the above example you provided me, but i'm
afraid even with your approach which should have given more error
specific information the output of the err file remains.


(e-mail address removed) [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')
 
F

Ferrous Cranus

Στις 1/9/2013 11:35 πμ, ο/η Steven D'Aprano έγÏαψε:
Τη Σάββατο, 31 ΑυγοÏστου 2013 9:41:27 Ï€.μ. UTC+3, ο χÏήστης Ferrous
Cranus έγÏαψε:

Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of
them. I have study all of your examples and then re-view my code and i
can *assure* you that the line statement that tied to set the 'host'
variable is very early at the top of the script(of course after
imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee
before actually making one first but rather than i cannot drink the
coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ


You are mistaken. In line 20-25, you have this:

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'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
or "Proxy Detected"
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what
the error is? You have access to the err.out file, but apparently you
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of "host"
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and
fix it.

But i'm Steven! That why i make use of it to read it immediately after
my script run at browser time.

i have even included a sys.exit(0) after the try:/except block:

Here is it:


errout = open( '/tmp/err.out', 'w' ) # opens and truncates the error
output file
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'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy
Detected"
except Exception as e:
print( "Xyzzy exception-", repr( sys.exc_info() ), file=errout )
errout.flush()

sys.exit(0)

and the output of error file is:


(e-mail address removed) [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,802
Messages
2,569,662
Members
45,433
Latest member
andrewartemow

Latest Threads

Top