User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

python 3 cgi fails when file read

Wed Mar 08, 2017 2:28 am

My python cgi on apache ran fine until I added these two lines:

Code: Select all

fileread = open('linesOfText', 'r')
buff = fileread.read()
They run fine in a terminal, but not thru cgi.
So I figure its some kind of security issue. Anyone know why?
Antikythera

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: python 3 cgi fails when file read

Wed Mar 08, 2017 1:20 pm

Hello,
most possibly there are some error output lines in apache logs which are in /var/log/apache2
If not, then use syslog and a try/except logic like:

Code: Select all

try: 
    file open code...
except Exception as e:
    import syslog
    syslog.syslog( str(e) )
and then errors are written to /var/log/syslog

Hope this helps
Gerhard

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: python 3 cgi fails when file read

Wed Mar 08, 2017 7:27 pm

Thanks!
But I figured it out. It needed the full path.
Antikythera

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: python 3 cgi fails when file read

Mon Mar 13, 2017 12:59 am

I like to use

Code: Select all

with open(FileName, 'rt') as file_obj:
    FileData = file_obj.read()
    file_obj.close()
The other question is it the second time you tried to enter the file?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Python”