I'm using the telnetlib to read a text file from a webserver and push out the lines to an LCDProc server rotating them every few seconds. Everything so far works ok unless there is a space in the imported line. I've read the python documentation and searched but I'm not finding anything that helps, I'm very new to Python which doesn't help.
The 'widget_set G 1 1 1' specifies where the following text should go, after that I add in what I want to display, this works ok but I don't understand it. The %s appears to insert a space which I am guessing is a command native to the LCDproc server not Python? '\n' is a new line? I don't see why I need one on a single line of the display. However, the time is formatted with spaces in and this displays correctly:
tn.write("widget_set G 1 1 1 \" %s \"\n" % (cur))
The problem is on the second line of the display, I've tried various combinations of \" and " with mixed results but I can't get it to display lines with spaces in (they just come up blank) and sometimes it misses off the last line. What should I write here to make it send the 'line' string? I did have it working at one point using '\"' somewhere but then changed some of the code and now can't get it to work again?
tn.write("widget_set G 2 1 2 " + line)
With enough patience I can normally Google until I find an answer but I'm pulling my hair out here!
If it has any bearing I removed the following line after every tn.write line because it appeared to be filling the data variable up with endless lines of data that wasn't being used for anything:
data += tn.read_until("\n");
Full code for reference:
#!/usr/bin/python
import telnetlib;
import time;
import urllib2;
host='127.0.0.1';
port='13666';
tn = telnetlib.Telnet(host, port)
tn.write("hello\r");
tn.write("screen_add G\n");
tn.write("widget_add G 1 string\n");
tn.write("widget_add G 2 string\n");
var = 1;
while var == 1 :
file = urllib2.urlopen("http://192.168.100.17/test.txt")
for line in file.readlines():
time.ctime()
cur = time.strftime('%l:%M:%S %p')
tn.write("widget_set G 1 1 1 \" %s \"\n" % (cur))
tn.write("widget_set G 2 1 2 " + line)
time.sleep(1)
So, you are saying that this line works correctly:
tn.write("widget_set G 1 1 1 \" %s \"\n" % (cur))
And this line fails?
tn.write("widget_set G 2 1 2 " + line)
So, do what the first line does. Try this:
tn.write("widget_set G 2 1 2 \" %s \"\n" % (line))
If it is easier to read, the following line is equivalent. (Note how the stirng uses '
instead of "
, so the interior "
s don't have to be escaped):
tn.write('widget_set G 2 1 2 " %s "\n' %(line))
Note that this will probably still fail if either cur
or line
have a quote character "
embedded therein.