3

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>&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 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();
}
5
  • If you want your Python script to masquerade as a browser and trigger the JavaScript function, you will probably need a full-fledged browser environment and JavaScript engine in Python, which would be quite a heavyweight requirement for your task. Can you read the source code of that ChangeState function 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. Commented Jun 3, 2015 at 9:19
  • I cleaned up the code for you a bit (not sure if it came without line breaks or if that was just a botched paste-job). It looks like ChangeState(1) is grabbing an HTML form called tForm, setting one of its entries to Math.abs(1 - sockstate[0]) and submitting that form. Does the HTML have any indication what happens when this tForm is submitted? Commented Jun 4, 2015 at 4:00
  • thanks a lot. @AhmedFasih . But my problem is what commands (of Javascript perhaps) I have to send from Python Shell to switch my sockets at my will. Commented Jun 5, 2015 at 23:11
  • What is the "comet" you're referring to? Is it this? Commented Jun 6, 2015 at 1:01
  • If my hunch is right, then the JavaScript code, by submit()ting that tForm, makes a request to the webserver, like http://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 use urllib to make a request to that same URL and have the same effect as the browser. urllib would open a connection to that URL, and if that wasn't sufficient, send a HTTP request header like GET /doCoolStuff?value=2 HTTP/1.1 etc. Commented Jun 6, 2015 at 4:18

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks the manufacturer doesn't have an API, and unfortunately they are not ready to cooperate and guide as I am kind of using a different approach than they provided. The user manual with the instrument is one of the crappiest pieces of manual I have come across in my life.
Understood. However, the code above is all you would need, aside from figuring out which "interesting button" you want to push. I think using comet might be overkill for this application.
@Ahmed Fasih and Charlesreid1 guys so I finally managed to program the socket board by sending url commands from urllib of python so thanks a lot. the main issue was to maintain the session after login of the device web portal. but anyway thanks a lot.
hi @charlesreid1 could I do this thing i mean url post method in matlab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.