0

I have a python cgi script that creates a text area and fills it with default value from the contents of a file. This used to work but recently I noticed that with change in content on the file ;the html is rendered incorrectly and the submit button and some parts of the file contents to be shown in the text area(as default content) etc is missing or messing up with the total page's html

 print('<form action="x.cgi" method="post">')
        print('<textarea name="textcontent" cols="120" rows="50">')
        with open('somefile', 'r') as content_file:
            content = content_file.read()
        content_file.close()
        print(content)
        print('</textarea>')
        print('<HR>')
        print('<input type="submit" value="Submit" />')
        print('</form>')

What can be done so that the contents of somefile doesnt mess with the html form . Note that somefile is a configuration file and I need everything in the file to be printed as such so user can make necessary change and submit it

Anoop P Alias
  • 373
  • 1
  • 6
  • 15
  • since you use `with`, the line `content_file.close()` is redundant, although not the problem. I do not see any problems with the code: the content of the file can indeed mess up the HTML syntax. – Pynchia Jan 16 '16 at 11:23
  • Is there a way around . I tried using urllib.quote ;but as I mentioned I need the contents of file intact in the text area . Or probably encrypt/encode the content in python and use javascript to decrypt/decode the content etc(just a thought) – Anoop P Alias Jan 16 '16 at 12:02
  • 1
    See http://stackoverflow.com/a/2502462/4014959 – PM 2Ring Jan 16 '16 at 12:14
  • 1
    Also see [What's the easiest way to escape HTML in Python?](http://stackoverflow.com/q/1061697/4014959) – PM 2Ring Jan 16 '16 at 12:26

0 Answers0