I checked the server logs and I can't seem to find anything that explains why it's doing this. Every time I load the page I get a "500 Internal Server Error" message. All I'm trying to do is update a JSON file.
#!/usr/bin/env python
import cgi
import json
new_data = {"2": {"title": "what", "date": "tomorrow"}}
with open("jobs.json") as file:
data = json.load(file)
data.update(new_data)
with open('test.json', 'w') as file:
json.dump(data, file)
You need to send some content type header first before sending anything, from Apache's docs :
There are two main differences between ``regular'' programming, and CGI programming.
First, all output from your CGI program must be preceded by a MIME-type header . This is HTTP header that tells the client what sort of content it is receiving. Most of the time, this will look like:
Content-type: text/html
so it would be something like:
#!/usr/bin/env python
import cgi
import json
import sys
new_data = {"2": {"title": "what", "date": "tomorrow"}}
# print "Content-type: application/json\r\n\r\n" to the output stream
sys.stdout.write("Content-type: application/json\r\n\r\n") # read the comment
with open("jobs.json") as file:
data = json.load(file)
data.update(new_data)
with open('test.json', 'w') as file:
json.dump(data, file)
also check that path to these files and that you write to a writable directory.
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.