sending javascript command from python shell -


i have device, board of 4 programmable electric sockets exact. device's interface pc through tcp/ip port. device has web browser user interface, accessible @ local ip address.

there javascript code on interface's html page reckon making webpage interactive , defining behaviour of page. firstly, can send javascript commands python switch sockets using web interface (if have tcp/ip connection server opened python.) so, need call javascript function in python shell. how that? secondly, need comet in case, because need push javascript command particular ip , port python.

here python code :

import socket  import time  tcp_ip = 'xxx.xxx.y.zzz'  tcp_port = wwww  message1 = "xxx.xxx.y.zzz/";  message2 = "javascript: changestate('1')"  s = socket.socket(socket.af_inet, socket.sock_stream) s.connect((tcp_ip, tcp_port))  s.send(message1) s.send(message2)  s.close()  

and javascript function:

function changestate(sn) {   f = document.forms.tform;   ind = sn * 1 - 1;   f["cte" + sn].value = math.abs(1 - sockstates[ind]);   f.submit(); } function activatedeactivate() {   f = document.forms.tform2;   f["activate"].value = actbtn;   f.submit(); } function timerfunction() {   cleartimeout(timer);   if (trycon == 1 && active == 1) {     document.location.href = "xyz.html";   } } function starttimer() { timer = settimeout(timerfunction, period); } window.onload = function() {   (i = 0; < 4; i++) {     if (sockstates[i] == 0) {       clsname = 'offstate';       str1 = 'off';       str2 = 'on';     } else {       clsname = 'onstate';       str1 = 'on';       str2 = 'off';     }     strhtml = '<span class="' + clsname + '">' + str1 +               '</span>&nbsp;<a href="javascript: changestate(\'' + (i + 1) +               '\')" class="onoffbtn">' + str2 + '</a>';     el = document.getelementbyid('stcont' + i);     el.innerhtml = strhtml;   }   stata = '';   statb = '';   statc = '';   rmsg = '';   if (ipid != 0) {     stata = "registered - ";     tmpel = document.getelementbyid('regbtn');     tmpel.innerhtml = 'login';   } else {     rmsg = "register manage ab-xyz-lan internet ( free service )";   }   if (active == 1) {     statb = "activated - ";   } else {     statb = "not activated";   }   if (active == 1) {     if (trycon == 1) {       statc = "trying connect";     } else if (serv == 1) {       statc = "connected";     } else if (serv == 0) {       statc = "not connected";     }   }   statael = document.getelementbyid('statusa');   statael.innerhtml = stata;   statbel = document.getelementbyid('statusb');   statbel.innerhtml = statb;   statcel = document.getelementbyid('statusc');   statcel.innerhtml = statc;   rmsgel = document.getelementbyid('regmsg');   rmsgel.innerhtml = rmsg;   actbtnel = document.getelementbyid("actbtn");   if (actbtn == 1) {     actbtnel.innerhtml = 'activate';   } else {     actbtnel.innerhtml = 'deactivate';   }   regbtnel = document.getelementbyid("regbtn");   regbtnel.href = "http://www.example.com/user/register.aspx?mac=" + mac;   if (warn == 1) {     alert("failed connect. please, check dns server settings.");   }   if (warn == 2) {     alert("failed activate. please, check, device registered.");   }   starttimer(); } 

i suggest using selenium , python bindings, allow control browser instance python. enable click button or call javascript function python script.

your script include code this:

from selenium import webdriver selenium.webdriver.common.keys import keys  driver = webdriver.firefox() driver.get("http://xx.yy.zz") elem = driver.find_element_by_name("interesting_button") elem.send_keys(keys.return) 

this solution. however, it's bit hackish. more elegant solution figure out javascript functions doing behind scenes. they'll calling other code, or sending message listener. manufacturer may have api, allow use python library urllib call api. or, may have specifications on how communicate device rs-232 (serial). i'd try google search device make/model of keywords , see comes up.


Comments