I have a device, a board of four programmable electric sockets to be exact. The device's interface with the pc is through the TCP/IP port. The device has a web browser user interface, accessible at a local IP address.
There is Javascript code on the interface's HTML page which I reckon is making the webpage interactive and defining the behaviour of the page. Firstly, can I send Javascript commands from Python to switch the sockets using the web interface (if I have TCP/IP connection to the server opened from Python.) So, for that I would need to call a Javascript function in a Python shell. How to do that? Secondly, do I need comet in this case, because I need to push Javascript command to a particular IP and port from Python.
Here is my 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 this is the 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() {
for (i = 0; i < 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> <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 to manage AB-xyz-LAN from Internet ( free service )";
}
if (active == 1) {
statB = "Activated - ";
} else {
statB = "Not activated";
}
if (active == 1) {
if (trycon == 1) {
statC = "Trying to 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 to connect. Please, check DNS server settings.");
}
if (warn == 2) {
alert("Failed to activate. Please, check, that device is registered.");
}
StartTimer();
}
ChangeStatefunction and see what it does? It's probably just making a request to a specific route on the web server which triggers an action on the hardware. Hopefully it's much easier to port that function to Python.ChangeState(1)is grabbing an HTML form calledtForm, setting one of its entries toMath.abs(1 - sockstate[0])and submitting that form. Does the HTML have any indication what happens when thistFormis submitted?submit()ting thattForm, makes a request to the webserver, likehttp://192.168.0.1/doCoolStuff?value=2(just a total random example), and the hardware does something. As @charlesreid1 says in their answer, you could useurllibto make a request to that same URL and have the same effect as the browser.urllibwould open a connection to that URL, and if that wasn't sufficient, send a HTTP request header likeGET /doCoolStuff?value=2 HTTP/1.1etc.