0

Following the example given here I am trying to setup a simple client/webserver to use javascript as client and python as server. However, I seem to do something wrong, as I do not get an output on the server side (and no error message in the javascript console).

The complete files are exampleClient.html:

<html>
<body>
<script src="jquery.js"></script>
<script>
    function toPython(usrdata){
    $.ajax({
        url: "localhost:8082",
        type: "POST",
        data: { information : "You have a very nice website, sir." , userdata : usrdata },
        dataType: "json",
        success: function(data) {
           //s <!-- do something here -->
            $('#somediv').html(data);
        }});
    }
    $("#filter").click(toPython("something5"));
    //$("#onclick").bind('click', toPython("something5"));


</script>

<input type="button" id="filter" name="filter" value="Filter" />
<p id="demo"></p>


</body>
</html>

and server.py:

# Python and Gevent
from gevent.pywsgi import WSGIServer
from gevent import monkey
monkey.patch_all() # makes many blocking calls asynchronous

def application(environ, start_response):
    if environ["REQUEST_METHOD"]!="POST": # your JS uses post, so if it isn't post, it isn't you
        start_response("403 Forbidden", [("Content-Type", "text/html; charset=utf-8")])
        return "403 Forbidden"
    start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
    r = environ["wsgi.input"].read() # get the post data
    print(r)
    return r

address = "localhost", 8082
server = WSGIServer(address, application)
server.backlog = 256
server.serve_forever()

I would appreciate help to fix the problem, or if you have another working example of how to create such a client/server setup...

4
  • Try putting the script at the end of the body. i.e. after <p> tag. Commented Jul 23, 2017 at 13:51
  • @ShreyashSSarnayak: Thanks for the suggestion, but no change Commented Jul 23, 2017 at 13:59
  • Try to use a protocol in javascript url: "http://localhost:8082" Commented Jul 23, 2017 at 14:32
  • @MauriceMeyer: It kind of works, but not in the way intended. I start the server, then I Load the html (without clicking on it) and then I get an error I cannot paste here. Maybe you have an idea on where I can find a REAL and WORKING example? Commented Jul 24, 2017 at 8:45

0

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.