4

I have an HTML form with data that I would like to send to my SVN. Since HTML/JS have no means of doing this, I want to use Python as a link between the form and the SVN. My problem is that I do not know how to send data from HTML/JS to Python, both of which are client side (there is no server involved).

What I imagined would happen is that a user would fill out the form, then press a 'submit' button, which would call a Python script and pass their form data as arguements.

I have been searching and found that people are running Python server side and POSTing to it from their javascript, but since I have no server I don't think this is possible for me.

Is it possible to send data from HTML/JS to Python if they are both client side?


EDIT: I should add that I do have a good background in Python and JS

8
  • 2
    SVN as in subversion ? Commented Feb 11, 2013 at 18:11
  • Are you familar with javascript and AJAX? Commented Feb 11, 2013 at 18:15
  • @JonathanVanasco Yes that is what I meant. Commented Feb 11, 2013 at 18:22
  • @Torxed no, I am not familiar with AJAX Commented Feb 11, 2013 at 18:22
  • See my solution, if you know how to develop things, you'll most likely succeed in putting the peaces togeather :) Commented Feb 11, 2013 at 18:24

1 Answer 1

2

Here's a few neat ways of combining Python with JavaScript:

Return data from html/js to python

Note: Since you mentioned you had no server, the request you call with the javascript has to be pointed to the listening port of the socket that the python code runs on. Easy enouhg would be to listen on port 80 with python and just do regular calls without thinking twice to the :80 from JavaScript.

Basically, HTML form, use JavaScript onSubmit() or a button that calls the AJAX code in the post above, then have Python read the JSON data (structure the <form> data according to the JSON format shown at the top of the link)


Here's a short intro on how to use form data via javascript:

<HTML>
    <HEAD>
        <TITLE>Test Input</TITLE>
        <SCRIPT LANGUAGE="JavaScript">
        function testResults (form) {
            var TestVar = form.inputbox.value;
            alert ("You typed: " + TestVar);
        }
        </SCRIPT>
    </HEAD>
    <BODY>
        <FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
            <INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
            <INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
        </FORM>
    </BODY>
</HTML>

Use this principle to gather your information,
then build in the AJAX part in the link mentioned at the top..
Once you've done that, start a python script (shown in the link as well) that listens for these calls.

Remember: To use JSON, format it properly, ' will not be allowed for instance, it has to be "!


In my link, this is the important part that sends the GET request to the "server" (python script):

xmlhttp.open("GET","Form-data",true);

Here's the python part:

from socket import *
import json
s = socket()
s.bind(('', 80)) # <-- Since the GET request will be sent to port 80 most likely
s.listen(4)
ns, na = s.accept()

while 1:
    try:
        data = ns.recv(8192) # <-- Get the browser data
    except:
        ns.close()
        s.close()
        break

    ## ---------- NOTE ------------ ##
    ## "data" by default contains a bunch of HTTP headers
    ## You need to get rid of those and parse the HTML data,
    ## the best way is to either just "print data" and see
    ## what it contains, or just try to find a HTTP parser lib (server side)    

    data = json.loads(data)
    print data
Sign up to request clarification or add additional context in comments.

4 Comments

Hehe, yw ;) Remember, there's a lot of mechanics involved here. You have the parsing of forms data, dynamicly sending requests via JavaScript (or replace the AJAX part with a regular HTML form submit to the IP of the python script), Python socket, Python parsing HTTP data, python converting GET requests to JSON strings and finally Python talking to SVN :)
In the python script, apparently I don't have the permission to access port 80. I can see how to switch the port number in the python script, but how do i specify a different port in the JS ?
And is there any way to test if the data is being transmitted from the JS correctly? (trouble shooting)
As far as trouble shooting goes, after the ns.recv(8192) just do a print on that data, you'll see if the transmitted data is correct or not, and if you don't see a print.. no data was transmitted (logic). And as for port number, well, just do xmlhttp.open("GET","http://ip.addr.com:666/wham.php",true);, this should result in you getting a request for wham.php on ip.addr.com on port 666

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.