Passing form data to python

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

I would assume that the user fills in the form, then press the submit button, which will call the Python script and pass their form data as arguments.

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

Is it possible to send data from HTML / JS to Python if it is client-side?


EDIT: I must add that I have a nice background in Python and JS

+3


source to share


1 answer


Here are some neat ways to combine Python with JavaScript:

Return data from html / js in python

Note. ... Since you mentioned that you don't have a server, the request you are making with javascript must point to the listening port of the socket that the python code is running on. The lightweight enouhg will listen on port 80 with python and just make normal calls without thinking twice: 80 from JavaScript.

Basically, in HTML form use JavaScript onSubmit()

or a buttonwhich calls the code AJAX

in the above post, and then Python reads the data JSON

(structure the <form>

data as JSON format

shown at the top of the link).


Here's a short introduction 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 information,
then create an AJAX piece in the link mentioned above.
Once you have done that, run the python script (as shown in the link) that listens for these calls.



Remember: To use JSON , formatting it correctly '

will not be allowed, for example it should 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

      

+2


source







All Articles