Python Forum
Dict KeyError in cgi-bin script, works fine via console
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dict KeyError in cgi-bin script, works fine via console
#1
Hi all, I'm experiencing a KeyError when I try to run the following cgi-bin script via my web form. The error indicates the problem resides on line 13 of the script: the key, 'yellow', was not found, even though it's clearly shown in the error message output (it shows the contents of the loaded dict).

#!/usr/bin/env python3.5
import cgi
import cgitb
import pickle
import html

cgitb.enable()
form = cgi.FieldStorage()

print("Content-type: text/html\n\n")

userVars = pickle.load(open("/opt/RampPi/RampPi.CFG", "rb"))
userVars['yellow'] = int(form["yellow"].value)
pickle.dump(userVars, open("/opt/RampPi/RampPi.CFG", "wb"))
text = form["yellow"].value
print("<h1>Save successful</h1>")
print(html.escape(text))
What's interesting is that if I run the exact same commands in the Python3.5 console, it works perfectly:
$ python3.5
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> userVars = pickle.load(open("/opt/RampPi/RampPi.CFG", "rb"))
>>> userVars['yellow']
10
>>> userVars['yellow'] = int(15)
>>> userVars['yellow']
15
>>> userVars['yellow'] = int(10)
>>> userVars['yellow']
10
Permissions on /opt/RampPi/RampPi.CFG are rw-r--r--, and it's owned by the current user (myself). /usr/lib/cgi-bin/ (the location of the script) is owned by root but permissions are rwxr-xr-x. Not sure if that's a factor here but I figure it can't hurt. I would think that if it were a permissions error I would get a different message, but who knows!

I'd appreciate your help. Thanks!
Reply
#2
You use 'yellow' as a key twice on that line. Are you sure the error is with userVars and not form?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
That could be something... I named the form variable "yellow" as well just because I will have multiple items on the form, and all they do is set the values of corresponding items in the dict, so I wanted to make life easy, so to speak.

Wow, that was dumb...changing the form variable from yellow to yellowIn did it. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Combine console script + GUI (tkinter) dejot 2 361 Feb-27-2024, 04:38 PM
Last Post: deanhystad
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,189 May-05-2022, 12:32 PM
Last Post: H84Gabor
  How to fine tune cutecharts? Gigux 1 1,205 Jan-28-2022, 05:41 PM
Last Post: Larz60+
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,930 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Encountering `importlib_metadata.PackageNotFoundError` trying to run console script gretchenfrage 0 5,543 Jul-08-2021, 09:26 PM
Last Post: gretchenfrage
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,544 Jun-27-2021, 08:30 PM
Last Post: Hpao
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 2,475 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,372 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  IPython console vs Spyder script losc 3 2,673 Apr-30-2020, 04:57 AM
Last Post: deanhystad
  script works in windows but not in linux ovidius80 2 2,679 Apr-29-2020, 02:10 PM
Last Post: ovidius80

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020