Execute a python script stored on a web server using beautiful soup
Question: I am trying to create a simple form/script combination that will allow someone
to replace the contents of a certain div in an html file with the text they
input in an html form on a separate page.

The script works fine if everything is local : the script is local, i set the
working directory to where my html file is, and i pass the parameter myself
when I run the script. When I load everything to my hosted site server,
however, it gives me a 500 error.

I have been able to execute a simple python script that i stored on my site,
and JustHost, my hosting service, has told me that BeuatifulSoup has been
added to my server.

Here is the script, with the parameter "textcontent" coming from an html form
which works fine. My scirpt is rooted under public_html/cgi-bin/ and the html
I am trying to read and write resides on the root of public_html. I'm guessing
either the html file isn't being found or beautifulsoup isn't actually
available on my server...anyway way to test that??

    
    
    #!/usr/bin/python
    #import beautifulsoup
    from BeautifulSoup import BeautifulSoup
    # Import modules for CGI handling 
    import cgi, cgitb, traceback
    
    
    # Create instance of FieldStorage 
    try:
        form = cgi.FieldStorage()    
        def text_replace(word):
        f = open('/public_html/souptest2.html', 'r')
        soup = BeautifulSoup(f.read())
        f.close()
    
        text = soup.find('div', attrs={'id': 'sampletext'}).string
        text.replaceWith(word)
    
        deploy_html = open('/public_html/souptest2.html', 'w')
        deploy_html.write(str(soup))
        deploy_html.close()
    
    # Get data from fields
        if form.getvalue('textcontent'):
            text_content = form.getvalue('textcontent')
            text_replace(text_content)
        else:
            text_content = "Not entered"
    except:
        deploy_html = open('../souptest2.html', 'w')
        traceback.print_exc(deploy_html)
        deploy_html.close() 
    

I have tried to load that as a script and run it from a url and still get a
500 error, with no output on my output page in order to debug using
traceback...


Answer: Do you have a shell account on your host? If so, try running the server's
version of Python in interactive mode and type:

    
    
    >>> import BeautifulSoup
    

If the module doesn't exist, you should get an `ImportError`.

Alternatively, try putting `cgitb.enable()` immediately following the line
`import cgi, cgitb`. This _should_ give you the traceback of any exceptions
raised. If this _still_ doesn't work, it's likely that your webserver isn't
finding the script. Double-check your configuration with your hosting
provider.

There is a whole bunch of advice on how to debug CGI scripts in Python's CGI
documentation, found [here](http://docs.python.org/library/cgi.html).

**EDIT** : Edited to actually take advantage of the `cgitb` module rather than
try to handroll a solution using `traceback`.

