0

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)
Doug Smith
  • 29,668
  • 57
  • 204
  • 388

1 Answers1

2

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.

  • How come it's not updating my JSON then? – Doug Smith Nov 25 '13 at 19:25
  • @DougSmith That probably has to do with the paths and/or the right access, try to write full path to writable directory. –  Nov 25 '13 at 19:35
  • The print should end with `\n\n`, not what you have above. – Robᵩ Nov 25 '13 at 19:35
  • @BSH - you are correct, I overlooked the implicit `\n`. But the `\r` is still incorrect. – Robᵩ Nov 25 '13 at 19:37
  • @Robᵩ Actually the correct form is `\r\n\r\n`, [HTTP header line break style](http://stackoverflow.com/questions/5757290/http-header-line-break-style). –  Nov 25 '13 at 19:45
  • Yes, but HTTP is not CGI. The correct form for a CGI script is `\n\n`, according to [the document you cited](http://httpd.apache.org/docs/2.2/howto/cgi.html#writing). According to [RFC3875](http://tools.ietf.org/html/rfc3875#section-2.2), lines are ended with the system-dependent new line sequence. In Python, that is always '\n'. Regardless, if you run your script on Windows, you'll get CR CR NL CR CR NL on the output stream, which neither of us claim is correct. – Robᵩ Nov 25 '13 at 19:58
  • @Robᵩ The document doesn't seem to follow the standard here. Apache tolerates `\n\n` and fixes it to `\r\n\r\n`, from [Apache: The Definitive Guide](http://docstore.mik.ua/orelly/linux/apache/ch04_02.htm): "*A CGI script consists of headers and a body. Everything up to the first blank line (**strictly speaking, CRLF CRLF, but Apache will tolerate LF LF**) is header, and everything else is body.*". In Windows you will get `\r\n\r\n`, and if you used `\n\n` it will be fixed to `\r\n\r\n`, this is a [test case](http://codepad.org/vDXyE5Os). –  Nov 25 '13 at 20:49