LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2005, 03:00 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Python CGI script can't write files, permission denied


Ok, I have my python CGI script, and it isn't able to write to my config file (which is named config.cfg). First it wasn't able to open the file at all. So I did a touch config.cfg, and a chmod 777 config.cfg, but still it can't open the file for writing ("Permission denied"). So, since it was in my ScriptAlias directory, I thought maybe that was the problem, so I made a new directory, and put it in that, so now it is in test/config.cfg. It still wont work! I've tried changing users, modes, everything on the file and all the directories leading to the file, but still I get the same error. Anyone got any ideas?
 
Old 03-13-2005, 10:47 AM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Can you post some code?
 
Old 03-14-2005, 12:17 AM   #3
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Code:
#!/usr/bin/python

import cgi, cgitb; cgitb.enable()

print "Content-Type: text/html\n\n"     # HTML is following
#print                               # blank line, end of headers

class IncomeChunk:
	Type = 0
	Amount = 0.00
	Description = ""
	
IncomeSplits = []

def LoadCFG():
	global IncomeSplits
	
	try:
		Handle = open("accounting/config.cfg", "rb")
	except:
		print "Error: Unable to open config file for reading"
		return
	
	try:		
		Line = Handle.readline()
		Splits = Line.split(",")
		del IncomeSplits[:]
		for I in Splits:
			Data = I.split(":")
			Chunk = IncomeChunk()
			Chunk.Type = int(Data[0])
			Chunk.Amount = float(Data[1])
			Chunk.Description = str(Data[2])
			IncomeSplits.append(Chunk)
	except:
		print "Error: Unable to load configuration"
		return
	Handle.close()
		
def SaveCFG():
	global IncomeSplits
	
	Handle = open("accounting/config.cfg", "wb")
	#except:
	#	print "Error: Unable to open config file for writing"
	#	return
		
	for I in IncomeSplits:
		if I != IncomeSplits[0]: Handle.write(",")
		Handle.write("%d:%f:%s"%(I.Type, I.Amount, I.Description))
	Handle.write("\n")
	Handle.close()

Form = cgi.FieldStorage()
Action = Form.getvalue("_cmd")		
print "<html><body bgcolor=\"#205fd4\" text=\"#ffffff\">"
LoadCFG()

if Action == "add_split":
	Amount = Form.getvalue("amount")
	Description = Form.getvalue("description")
	if '%' in str(Amount):
		Chunk = IncomeChunk()
		Chunk.Type = int(0)
		Chunk.Amount = float(Amount.strip("%%"))
		Chunk.Description = str(Description)
	else:
		Chunk = IncomeChunk()
		Chunk.Type = int(1)
		Chunk.Amount = float(Amount)
		Chunk.Description = str(Description)
	IncomeSplits.append(Chunk)
	SaveCFG()
	
print "<table border=\"1\" with=\"100%%\">"
print "<tr>"
print "<td>"
print "Amount - Amount of money for this split (put a %% for a percentage)"
print "</td>"
print "<td>"
print "Description - Description of split &nbsp;&nbsp;&nbsp; "
print "</td>"
print "<td>"
print "</td>"
print "</tr>"
Index = 0
while(Index < len(IncomeSplits)):
	I = IncomeSplits[Index]
	print "<form method=\"post\" action=\"accounting.py\">"
	print "<tr>"
	print "<td>"
	if I.Type == 0:
		print "<input type=\"text\" value=\"%s%%\" name=\"amount\">"%str(I.Amount)
	elif I.Type == 1:
		print "<input type=\"text\" value=\"%s$\" name=\"amount\">"%str(I.Amount)
	print "</td>"
	print "<td>"
	print "<input type=\"text\" value=\"%s\" name=\"description\">"%I.Description
	print "</td>"
	print "<td>"
	print "<input type=\"submit\" value=\"Edit\">"
	print "</td>"
	print "</tr>"
	print "<input type=\"hidden\" name=\"_cmd\" value=\"edit_split\">"
	print "<input type=\"hidden\" name=\"_index\" value=\"%d\">"%Index
	print "</form>"
	Index = Index + 1

print "<form method=\"post\" action=\"accounting.py\">"
print "<tr>"
print "<td>"
print "<input type=\"text\" value=\"\" name=\"amount\" size=\"50\">"
print "</td>"
print "<td>"
print "<input type=\"text\" value=\"\" name=\"description\" size=\"100\">"
print "</td>"
print "<td>"
print "<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\">"
print "</td>"
print "</tr>"
print "<input type=\"hidden\" name=\"_cmd\" value=\"add_split\">"
print "</form>"
print "</table>"
print "</body></html>"

That is my complete script, however the problem is in SaveCFG. It is going to be a script to help me split my income into percentages or raw amounts.
 
Old 03-14-2005, 05:55 AM   #4
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Have you tried changing up the modes on your Handles?

Handle = open("accounting/config.cfg", "w")

Also, and this may not make a difference, try putting the full path to config.cfg. I've had some issues with the open method as well and it seems to be very picky at some times.
 
Old 03-17-2005, 12:19 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Still doesn't work. I've tried everything, including all the things you guys suggested. So I just put my script on another server, and it works fine! Sheesh! Anyhow, thanks for the replies. If anybody does truely know the problem, than let me know! Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cgi-bin error (Permission Denied) Help! Amorgos Programming 8 09-08-2008 10:20 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM
Permission denied when trying to write to ttyS0 thepoint Linux - General 0 06-03-2004 09:36 PM
permission denied when executing my own script Red Squirrel Linux - Newbie 3 03-07-2004 08:21 PM
Keep getting "Permission Denied" when trying to write files dunnd40 Debian 2 02-01-2004 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration