I have an html form which is populated by javascript functions. I have another javascript function which gets the values of all of the form elements, and currently just uses an alert to display them back to the user. The html page is diplayed via python with the following code:
import webbrowser
new = 2 #open new tab if possible
url = "form.html"
webbrowser.open(url, new=new)
This all works well, but instead of displaying the data using an alert, I would like to pass the data back to python, but dont know how. All of the data is stored in a javascript array, so I essentially just need to pass this one piece of data.
EDIT: I cannot use any external libraries.
Something like Ghost.py should be able to do what you want.
This allows evaluation of JavaScript.
Which should help.
I've used PhantomJS the JS headless webkit browser and this is a port and/or reworking of that using Python.
In my use case I just called PhantomJS from
subprocess.call
as I couldn't be bothered to install theGhost
dependencies.I just emitted JSon to
stdout
andjson.loads
on it.You can take the HTML data, and convert it into a dictionary, enabling you to do:
print x['x']
This is the starting point, create a socket in Python which listens to a port. Then have it recieve data.
In Javascript, open a socket which can connect to a port (the one Python listens to). Use, say: http://socket.io/
This is a pure socket-to-socket related issue?
A working relationship between Python and Javascript (on port 80):
There you got a socket listening to 80, connect to that and send whatever you want.
For instance, where you can send the form data as a string, replacing "Form-data" and the response from Python can be put into "myDiv" :)