LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python CGI script can't write files, permission denied (https://www.linuxquestions.org/questions/programming-9/python-cgi-script-cant-write-files-permission-denied-300846/)

The_Nerd 03-12-2005 03:00 PM

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?

puffinman 03-13-2005 10:47 AM

Can you post some code?

The_Nerd 03-14-2005 12:17 AM

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.

Crashed_Again 03-14-2005 05:55 AM

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.

The_Nerd 03-17-2005 12:19 PM

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! :confused: Sheesh! Anyhow, thanks for the replies. If anybody does truely know the problem, than let me know! Thanks!


All times are GMT -5. The time now is 07:49 PM.