top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to read Mozrepl Javascript result into a Python Variable?

+1 vote
383 views

With Mozrepl addon in Firefox and Python I do:

> import telnetlib
> tn = telnetlib.Telnet(r'127.0.0.1', 4242, 5)
> tn.read_eager()
'nWelcome to MozRepl.nn - If you get stuck at the "'
> tn.read_until("repl> ")
...snip...
> tn.write(r'alert(window.content.location.href)'+"n")

and I get an alert box with the URL of the active tab.

But how do I read that URL into a python variable? Something like tn.write(r';var zz = window.content.location.href'+ "n") but that doesn't get it into python.

posted Jul 13, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

This seems to be what you're after:
https://github.com/bard/mozrepl/wiki/Tutorial

By my reading of that, this should do it:

tn.write('window.content.location.hrefn')

It seems to function somewhat like Python's own interactive mode - a bare expression gets sent to you. Very convenient.

answer Jul 13, 2013 by anonymous
Similar Questions
0 votes

How to read/load the cmake file in python and access its elements. I have a scenario, where i need to load the make file and access its elements. I have tried reading the make file as text file and parsing it,but its not the ideal solution. Please let me know how to load the .mk file and access its elements in python.

0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

+1 vote

I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work.

import subprocess

p = subprocess.Popen("D:PythonPython27Scriptspip.exe list -o",
 stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT,
 bufsize=1,
 universal_newlines=True,
 shell=False)
for line in p.stdout:
 print line

When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command
line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking?

+2 votes

Is there a better way to do that:

def Read_CSV_File(filename):
 file = open(filename, "r")
 reader = csv.DictReader(file)
 line = 1
 for row in reader:
 if line < 6:
 reader.next()
 line++
# process the CSV
...