1

Currently I'm using this function to send my JSON from a chrome extension. This is the client code from javascript sending the data.

function callPython(){
    var url  = 'http://AWS_IPNUMBER/';
    var data = {'bob':'foo','paul':'dog'};
    $.ajax({
        url: url,
        type: 'POST',
        contentType:'application/json',
        data: JSON.stringify(data),
        dataType:'json',
        success: function(data){
            var jsonObj = $.parseJSON(data);
            alert(jsonObj.encPassword);
        },
        failure: function(errorMsg) {
            alert(errorMsg);
        }
    });
}

This is the server code for Python:

s = socket()
s.bind(('', 80))
s.listen(4)
ns, na = s.accept()

while True:
    try:
        data = ns.recv(8192)
    except:
        ns.close()
        s.close()
        break

    data = json.loads(data)
    print data

The problem is that although it is listening, data is empty at data = ns.recv(8192). Then data = json.loads(data) doesn't work since data is empty. Why is this? I thought it may be a problem with my security group on AWS but if I go to http://AWS_IPNUMBER/ I get the header from the browser while running the python script.

2 Answers 2

1

You may have better luck with a good framework like tornado or django.

I say this because in your code you are trying to parse an http POST with json.loads. HTTP isn't that simple. You will need to deal with the request and headers before you get to the body, and this can be spread out across multiple packets. Why try to reinvent the wheel when you can setup a standards compliant server from a well established project.

Sign up to request clarification or add additional context in comments.

1 Comment

Would that be required for something this "simple"(maybe it's not as simple as I thought). The chrome extension would be that the user fills a form, that data is sent to me and then it's saved in a database(I don't need to send anything back).
1

The data that $.ajax function will put is a complete HTTP request, which json.loads() won't understand. In this case you need to instantiate a HTTP server which will process the HTTP requests and then process the HTTP payload with json.loads().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.