Premature end of script headers in python script bluehost

445 views Asked by At

I'm hosting a website off of Bluehost and I'm trying to execute a python script on a button click using AJAX. However every time I click the button the server responds with a 500 error to the python script. More specifically this is the error:

[Sun Nov 30 17:49:26 2014] [error] [client 174.61.70.33] Premature end of script headers: Steap.py, referer: http://steap.co/

This is the AJAX code:

$(document).ready(function(){
            $("#steambutton").click(function() {
                DisplayLoadingDiv();
                $.ajax({
                    type: "POST",
                    url: "http://steap.co/cgi-bin/Steap.py",
                    data: {steamid: "<?php echo $steamprofile['steamid']?>"},
                    success: function(response) {
                        $("#steamdiv").html(response);  
                        $("#phisherbutton").show()
                    },
                    error: function(response) {
                        $("#steamdiv").html("An error occured. Please try again.");
                    }
                });
            });
});

Basically the div is showing "An error occured. Please try again." after 0.01 secs of execution, and the console shows that the server responded with a 500 error when trying to load the python script resource.

Steap.py:

http://pastebin.com/F9THyt3u

Any ideas as to why this is happening?

1

There are 1 answers

8
Savir On

I'm pretty sure that the issue is, that you're not properly forming an HTTP response in Steap.py. Particularly important is the HTTP/1.1 200 OK\n part (the Status Line)

I've set up in my computer a raw TCP socket implementation (using SocketServer) and it correctly renders a page in my web browser by doing the following:

    import datetime
    body=("<html><title>Hello</title>"
          "<body>"
          "<div id=\"div-name\">"
          "%s"
          "</br></br>"
          "</div>"
          "</body>"
          "</html>" % foo(bar))

    reply=("HTTP/1.1 200 OK\n"
           "Date: %s\n"
           "Content-Type: text/html\n"
           "Content-Length: %s\n"
           "Connection: close\n"
           "\n"
           "%s" 
           % (datetime.datetime.utcnow().isoformat(' '),
              len(body),
              body)
    )
    print reply

This looks nicely displayed in Chrome:

enter image description here

Needless to say, but this is my foo(bar) function:

def foo(bar):
    return "Test test"

All this said, I'd really, really study how to install a pre-made server (such as Django or Tornado) in your Bluehost "area" (or however you wanna call it). I lean towards Tornado (its usage is very basic, but it'll help a lot so you don't have to "manually" write headers and bodies by yourself)