showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import cgi, urllib2 |
| 4 | |
| 5 | script = """\ |
| 6 | Content-Type: text/javascript |
| 7 | |
| 8 | %(callback)s(%(result)s); |
| 9 | """ |
| 10 | |
| 11 | form = cgi.FieldStorage() |
| 12 | path = form['path'].value |
| 13 | callback = form['callback'].value |
| 14 | |
| 15 | try: |
| 16 | file_contents = urllib2.urlopen('http://localhost' + path).read() |
| 17 | escaped_contents = file_contents.replace( |
| 18 | '\\', '\\\\').replace( # get rid of backslashes |
| 19 | '"', r'\"').replace( # escape quotes |
| 20 | '\n', '\\n') # escape newlines |
| 21 | result = '{"contents" : "%s"}' % escaped_contents |
| 22 | except urllib2.HTTPError: |
| 23 | result = '{"error" : "File not found"}' |
| 24 | |
| 25 | print script % dict(callback=callback, result=result) |