save xml using python cgi script

May 27 2008

  • 6 replies.
  • This question is resolved, if you still have a question, you can re-open it by reacting to it.
  • This question was started by lyricist.
  • Last post by lyricist.
lyricist
user
27 May, 19:53
I'm trying to write a "save file" cgi script to service the HTTPTools.postXML call. My script is written in python and I have tested it with a direct call from a browser. The script currently just prints out all the request parameter names to a file (since the documentation doesn't specify the parameter names used for the POST call). However when the script is called from my save.js, it prints out some dummy text ok (to check it's actually getting called) but then throws an exception because the parameters list is empty.

Does anyone have an example save cgi script I could try out... just to get going on this one (shame it's not included in the examples)?
Sjoerd
Xopus Team
28 May, 00:27
Google tells me the post data can be read through sys.stdin. I hope this helps.
lyricist
user
28 May, 13:16
Thanks. That worked. Unfortunately I was testing in FireFox but whern I try to run the exact same code in IE it doesn't even get past calling HTTPTools.postXML. I just get the error:

The document "http://localhost/xopus/exampes/Simple/document.xml" could not be saved.
Error meassage: Unspecified error"
Do you want to try again?"

I've checked the Apache log and it doesn't show the POST call so it's failing somewhere in the HTTPTools.postXML function.

Any clues as to why this works in FireFox but not in IE?
Sjoerd
Xopus Team
28 May, 13:33
The only thing I can think of is some kind of security setting.

Could you show your save code? The error could be anywhere in your save method, it does not have to be in the postXML code.
lyricist
user
28 May, 13:55
It's not even getting to my cgi script and apache shows no log of the cgi call (which it does when called from FireFox). So looking at my save script is irrelevant. I've tried with a trivial script with nothing other than the content-type and it still fails in the same way.

Here's my save.js script... in case there's something wierd here...


IO.setSaveXMLFunction(function(xmlURI, xmlDocument){alert('>> Saving to:'+xmlURI);

var response = HTTPTools.postXML('/cgi-bin/savefile.py?path='+xmlURI,xmlDocument,'UTF8');

if( response) alert(response);

});


And here's my savefile.py script anyway. It's not elegant yet but it works. (Note: the indentation is being removed... just trust me that it is there correclty).


#!C:/Tools/Python25/python.exe

import cgi
import sys
import os

import cgitb; cgitb.enable(display=0, logdir="c:/temp")

def main():
print "Content-type: text/html\n"

root_path = 'C:/Program Files/Apache Group/Apache2/htdocs/'

if 'QUERY_STRING' in os.environ:
qs = os.environ['QUERY_STRING']
url = qs[qs.find('xopus'):]
path = root_path+url

file = open(path,'w')
file.write(sys.stdin.read())
file.close()
else:
print '<p>Save failed... No URL found</p>'

main()
Sjoerd
Xopus Team
28 May, 14:28
Could you try changing "UTF8" to "UTF-8"?
lyricist
user
28 May, 14:49
BINGO! That did it. Thanks lots!


React

HTML will be shown as HTML code.
Linebreaks and Links starting with http:// are automatically resolved