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

BloonsTowerDefence
  • 1,184
  • 2
  • 18
  • 43
  • 2
    SVN as in subversion ? – Jonathan Vanasco Feb 11 '13 at 18:11
  • Are you familar with javascript and AJAX? – Torxed Feb 11 '13 at 18:15
  • @JonathanVanasco Yes that is what I meant. – BloonsTowerDefence Feb 11 '13 at 18:22
  • @Torxed no, I am not familiar with AJAX – BloonsTowerDefence Feb 11 '13 at 18:22
  • See my solution, if you know how to develop things, you'll most likely succeed in putting the peaces togeather :) – Torxed Feb 11 '13 at 18:24
  • ok. so you want to send HTML/JS form data to a subversion repo. Is the SVN repo local or is it on another machine? Why would there be an HTML form with no server and a SVN repo? It sounds like you're trying to do something overly complex and unnecessary. it might be best for you to describe what/why you want to do this from a business-case perspective. There's probably a much easier way to accomplish what you want. – Jonathan Vanasco Feb 11 '13 at 18:39
  • That being said, for this to happen you would need to have a "server". The server could be on your client machine too, but you need to have a daemon running and actively waiting for connections to accept the payload from the browser and then act upon it. you can't really have the POST action , or javascript, trigger a python script ( or other app ) to execute. you need to have Python running and waiting. i say 'really', because it is possible to register protocol handlers with browsers... but this can be really complicated. ( think of how spotify urls open the app , etc ) – Jonathan Vanasco Feb 11 '13 at 18:41
  • All I want to do is have a form for people to enter some data, turn it into XML/YML then send it to the SVN. – BloonsTowerDefence Feb 11 '13 at 18:42

1 Answers1

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
Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 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` :) – Torxed Feb 11 '13 at 18:26
  • 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 ? – BloonsTowerDefence Feb 11 '13 at 18:34
  • And is there any way to test if the data is being transmitted from the JS correctly? (trouble shooting) – BloonsTowerDefence Feb 11 '13 at 19:32
  • 1
    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` – Torxed Feb 11 '13 at 20:32