Reputation: 49
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();
}
Upvotes: 3
Views: 1996
Reputation: 4821
I would suggest using Selenium and its Python bindings, which would allow you to control a browser instance from Python. This would enable you to click a button or call a Javascript function from your Python script.
Your script would include code like this:
from selenium import webdriver
from 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 will get you a solution. However, it's a bit hackish. The more elegant solution is to figure out what the Javascript functions are doing behind the scenes. They'll probably be calling other code, or sending a message to a listener. The manufacturer may have an API, which would allow you to use a Python library like urllib
to call the API. Or, they may have specifications on how to communicate with the device by RS-232 (serial). I'd try a Google search for the device make/model with some of those keywords and see what comes up.
Upvotes: 3