Pass information out of html to python -
PS is the inspiration for that on my iPhone and my laptop Sharing the same WiFi network (adhoc portable hotspot) and then using my iPhone's more accurate GPS for my laptop is on Google Earth. I finally want to write it in a km list file which constantly updates my current latitude and long time, probably there are easy ways to do this but I thought learning a little dragon would be a good idea.
My location.py file
#! / Python print "Content Type: Text / HTML \ n \ n") Print ("" "\">
Head> P. ID = "Demo"> Test: & Lt; / p & gt; & lt; script & gt; var x = document.getElementById ("demo"); function getLocation () {if (navigator.valocation) {navigator.geolocation.getCurrentPosition (showPosition);} and {X.innerHTML = "Geographic location is not supported by this browser.";}} Show show (position) {x.innerHTML = "latitude:" + position.coords.latitude + "
Longitude: "+ Position.coords.longitude;} window.onload = getLocation; & lt; / script>
and my server file (webserver.py)
<
You have to send a request back to break your location and then cgi.FileStorage
to get data on your script You must change your Javascript code a bit to send request to
I show you an example of sending the static data back (due to I do not have a geographic location on my PC: P), you need to change it to send right latitude, loan data (can be inside the function showPosition
?)
Next, changes to your dragon script (please check that there are also Javascript!):
#! / Usr / bin / env dragon import cgi form = cgi.fildstorage () lat = form.getvalue ('lat') lon = form.getvalue ('LAN') open ('location.txt', 'wt') In the form of F: if latitude: f.write (lat) f.write ("\ n") if lon: f.write (lon) f.close () print ("content-type: text / html \ n \ N ") print (" "" \ "> gt>> head> gt =" demo "> test: & lt; script And gt; var x = document.getElementById ("demo"); function getLocation () {// place this back to your dragon script var req = new XMLHttpRequest (); req.open ('GET', 'Location.py?lat = 1 & lon = 2 ', fail); req.send (zero) E); // --------------------------------------------- - (navigator.geolocation) {navigator.geolocation.getCurrentPosition (showPosition);} and {x.innerHTML = "geographic location is not supported by this browser."}}} Function showPosition (status) {x.innerHTML = "Latitude : "+ Position.coords.latitude +" & lt; br & gt; Longitude: "+ position.coords.longitude;} Window.onload = getLocation; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; / Body & gt; & Lt; / Html & gt; "" ")
If you run the code, you will see that you have the calls on the file location.txt .
You can split the process on two scripts (almost the right approach!) And send a request for your second script (probably save_location.py ).
Comments
Post a Comment