简体   繁体   中英

IOError: [Errno 2] No such file or directory when executing script from web browser

I have just started learning python and how to execute scripts from browser. I have a sample script where i am trying to create a json file and write dict data into the file. Below is my code. When i am executing this script from terminal i am able to see the file getting created and data dumped into file. But when i am trying to execute the same script from web browser i am seeing error in my apache error.log file as IOError: [Errno 2] No such file or directory.

Here is my code and i am using python 2.7:

#!/usr/bin/python
import os
import json

data = {"price": "$10"}

filepath = '/tmp/'
filename = filepath + 'my_data.json'
os.path.join(filename)
f = open(filename, "w+") # error at this line 
json.dump(data, f)
f.close()


print "Content-type: text/html\n\n"
print '''
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en" />
<title>Sample Home Page</title>
</head>
<body>
'''

print '''
<h1>
<BR> SAMPLE TEXT
</h1>
</body>
</html>

Error:

[Sun Aug 19 13:19:15.913003 2018] [cgi:error] [pid 970] [client ::1:50818] AH01215: Traceback (most recent call last):: /var/www/html/python/test3.py
[Sun Aug 19 13:19:15.913177 2018] [cgi:error] [pid 970] [client ::1:50818] AH01215:   File "/var/www/html/python/test3.py", line 10, in <module>: /var/www/html/python/test3.py
[Sun Aug 19 13:19:15.913237 2018] [cgi:error] [pid 970] [client ::1:50818] AH01215:     f = open(filename, "w+"): /var/www/html/python/test3.py
[Sun Aug 19 13:19:15.913347 2018] [cgi:error] [pid 970] [client ::1:50818] AH01215: IOError: [Errno 2] No such file or directory: '/tmp/my_data.json': /var/www/html/python/test3.py
[Sun Aug 19 13:19:15.915809 2018] [cgi:error] [pid 970] [client ::1:50818] End of script output before headers: test3.py

                                                                                                                       3,9           Top

Please help me if i have missed to learn something. I have searched stackoverflow and web for a solution. Found no solutions related to my problem.

your code work fine! I've not got any error on my system(Ubuntu 17.10).

You have not specified which operating system and HTTP server you use. This can be relevant, because I have seen errors similar to yours under CentOS 7.6 with Apache 2.4.

One possible reason could be that SElinux is enabled by default under CentOS, and it can prevent CGI scripts accessing certain directories. I learned this the hard way when I ported a small CGI-based application (a simple sign-up page) from a VM running Ubuntu. The CGI script at the heart of it used to write its output to /var/tmp/reg , with the reg subdirectory owned by the "apache" user, and 775 permission. This ran under Ubuntu perfectly for years, but gave me the error [Errno 2] No such file or directory under CentOS. (BTW this can be the reason why @GLinBoy did not get any errors under Ubuntu 17.10 either.)

My quick-and-dirty solution was to let the CGI script write files into a directory (with permissions 777) under my home directory. This is good enough for a small VM serving only in-house clients, but should not be used on anything serious, given the potential security implications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM