Discussion:
KeyError when trying to read http user agent string
(too old to reply)
Φώντας Λαδοπρακόπουλος
2015-02-13 20:04:15 UTC
Permalink
Hello,

Suddenly i started to get this error message without any chnage to my script,
just to the security of apache via cPanel by runnign EasyApache:


***@secure [~]# python3 /home/nikos/www/cgi-bin/metrites.py
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/cgitb.py", line 268, in __call__
self.handle((etype, evalue, etb))
File "/usr/local/lib/python3.4/cgitb.py", line 273, in handle
self.file.write(reset())
ValueError: underlying buffer has been detached

Original exception was:
Traceback (most recent call last):
File "/home/nikos/www/cgi-bin/metrites.py", line 20, in <module>
userinfo = os.environ['HTTP_USER_AGENT']
File "/usr/local/lib/python3.4/os.py", line 651, in __getitem__
raise KeyError(key) from None
KeyError: 'HTTP_USER_AGENT'
***@secure [~]#


Here is the original code in my script:

userinfo = os.environ['HTTP_USER_AGENT']
lastvisit = datetime.now().strftime( '%y-%m-%d %H:%M:%S' ) # MySQL datetime format
ref = os.environ.get( 'HTTP_REFERER', 'Δεν βρέθηκε Αναφορά!' ).replace( 'www.', '' )

Any ideas as to why this error is being caused and the http user afent script cannot be retrieved?

Apache's security won't let it load?
Φώντας Λαδοπρακόπουλος
2015-02-13 20:09:31 UTC
Permalink
After altering it to:

userinfo = os.environ.get('HTTP_USER_AGENT', 'Άγνωστος Περιηγητής')

So to avoid KeyErrors, the error message always is:


[Fri Feb 13 22:08:12 2015] [error] [client 78.87.102.243] Exception ignored in: <bound method Connection.__del__ of <pymysql.connections.Connection object at 0x7f510edda2b0>>
[Fri Feb 13 22:08:12 2015] [error] [client 78.87.102.243] Traceback (most recent call last):
[Fri Feb 13 22:08:12 2015] [error] [client 78.87.102.243] File "/usr/local/lib/python3.4/site-packages/pymysql/connections.py", line 645, in __del__
[Fri Feb 13 22:08:12 2015] [error] [client 78.87.102.243] File "/usr/local/lib/python3.4/site-packages/pymysql/connections.py", line 632, in close
[Fri Feb 13 22:08:12 2015] [error] [client 78.87.102.243] TypeError: 'NoneType' object is not callable
Loading...