[Tutor] TypeError: 'str' object is not callable
Chris Hallman
challman at gmail.com
Fri Feb 17 16:28:32 CET 2006
Here is my script:
send_file.py
import os, random, re, string, sys, telnetlib, threading, time
from time import strftime
from threading import Thread
class ConfigIT(threading.Thread):
def __init__(self,host):
Thread.__init__(self)
self.host = host
self.filename = filename
def run(self):
try:
tn = telnetlib.Telnet(self.host)
tn.read_until("Username: ", 7)
except:
output.write(self.host + " - not responding.\n")
output.flush()
return
tn.write(user + "\n")
tn.read_until("Password: ", 7)
tn.write(pswd + "\n")
(index, match, read) = tn.expect(["#"], 7)
if not match:
output.write(self.host + " - sign-on failure.\n")
output.flush()
return
tn.write("conf t\n")
rpath = (dirPath + "\\" + self.filename)
print rpath
input = file(rpath, "r")
for line in file(rpath):
time.sleep(random.uniform(0,2))
text = input.readline()
tn.write(text)
(index, match, read) = tn.expect(["%"], 1)
if match:
output.write(self.host + " - config error on line = " +
text)
output.flush()
return
tn.write("end\n")
tn.read_until(self.host.upper() + "#")
time.sleep(random.uniform(0,2))
# write config to NVRAM
tn.write("wr\n")
tn.read_until(self.host.upper() + "#")
time.sleep(random.uniform(0,2))
# write config to TFTP
tn.write("wr net\n")
tn.read_until("]?")
time.sleep(random.uniform(0,2))
tn.write("192.168.19.201\n")
tn.read_until("]?")
time.sleep(random.uniform(0,2))
tn.write("\n")
tn.read_until("[confirm]")
time.sleep(random.uniform(0,2))
tn.write("\n")
(index, match, read) = tn.expect(["OK"], 15)
if "Error opening tftp" in read:
output.write(self.host + " - config write net failed.\n")
output.flush()
return
# if host = *swa, then write vlan.dat to SYSMAN
m = match_swa.match(self.host)
if m:
tn.write("copy flash tftp\n")
tn.read_until("]? ")
time.sleep(random.uniform(0,2))
tn.write("vlan.dat\n")
tn.read_until("]? ")
time.sleep(random.uniform(0,2))
tn.write("192.168.19.201\n")
tn.read_until("dat]? ")
time.sleep(random.uniform(0,2))
tn.write(self.host + ".vlan.dat\n")
(index, match, read) = tn.expect(["OK"], 15)
if "Error opening tftp" in read:
output.write(self.host + " - VLAN write net failed.\n")
output.flush()
return
tn.write("exit\n")
def count_active():
""" returns the number of Getter threads that are alive """
num_active = 0
for thread in tlist:
if thread.isAlive():
num_active += 1
return num_active
threads = []
dirPath = (r"c:\temp\config")
dirList = os.listdir(dirPath)
match_swa = re.compile('^s\d\d\d\d[s][w][a]')
Max_Threads = 10
pswd = "Activ8"
tlist = []
user = "cworks"
logFile = (r"c:\logs\send_file.log")
output = file(logFile, "a")
output.write("\n" + "send_file script started -" + strftime(" %H:%M:%S %x")
+ "\n")
output.flush()
for file in dirList:
filename = file.lower()
host = filename.replace('.txt', '')
while count_active() >= Max_Threads:
time.sleep(1)
threads = ConfigIT(host)
tlist.append(threads)
threads.start()
for thread in tlist:
thread.join()
output.write("send_file script completed -" + strftime(" %H:%M:%S %x") +
"\n")
output.flush()
output.close()
Here is the error:
>pythonw -u "send_file.py"
c:\temp\config\rtr0544.txt
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\python24\lib\threading.py", line 442, in __bootstrap
self.run()
File "send_file.py", line 45, in run
input = file(rpath, "r")
TypeError: 'str' object is not callable
>Exit code: 0
The above script was created using the following script:
send_file.r1.py
#
# This script obtains a directory listing, strips the extensions and telnets
to the
# device (which is the filename in the directory). Then it writes the
commands in the
# file to the device, saves config and writes it back to SYSMAN. It can be
run using:
# python send_file.py
#
# Note: "os" is imported for future functionality.
#
# by: TCDH
# on: 10/17/05
# revised: 10/18/05 TCDH - Added logic to check for offline devices and
sign-on failures.
#
import os, re, string, sys, telnetlib, threading, time
from time import strftime
from threading import Thread
dirPath = (r"c:\temp\config")
dirList = os.listdir(dirPath)
pswd = "Activ8"
user = "cworks"
logFile = (r"c:\logs\send_file.log")
output = file(logFile, "a")
output.write("\n" + "send_file script started -" + strftime(" %H:%M:%S %x")
+ "\n")
output.flush()
class ConfigIT(threading.Thread):
def __init__(self,host):
Thread.__init__(self)
self.host = host
self.filename = filename
def run(self):
try:
tn = telnetlib.Telnet(self.host)
tn.read_until("Username: ", 7)
except:
output.write(self.host + ": not responding.\n")
return
tn.write(user + "\n")
tn.read_until("Password: ", 7)
tn.write(pswd + "\n")
(index, match, read) = tn.expect(["#"], 7)
if not match:
output.write(self.host + ": sign-on failure.\n")
return
tn.write("conf t\n")
rpath = (dirPath + "\\" + self.filename)
print rpath
input = file(rpath, "r")
for line in file(rpath):
tn.write(input.readline())
tn.write("end\n")
tn.read_until(self.host.upper() + "#")
tn.write("wr\n")
tn.read_until(self.host.upper() + "#")
tn.write("wr net\n")
tn.read_until("host []?")
tn.write("192.168.19.201\n")
tn.read_until("confg]? ")
tn.write("\n")
tn.read_until("[confirm]")
tn.write("\n")
(index, match, read) = tn.expect(["OK"], 15)
if "Error opening tftp" in read:
output.write(self.host + ": write net failed.\n")
return
tn.write("exit\n")
tlist = []
for host in dirList:
filename = host.lower()
host = host.lower()
host = host.replace('.txt', '')
current = ConfigIT(host)
tlist.append(current)
current.start()
for entry in tlist:
entry.join()
output.write("send_file script completed -" + strftime(" %H:%M:%S %x") +
"\n")
output.flush()
output.close()
They both use the same syntax to open the input file, however only
send_file.py receives an error. What gives?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060217/8c8ec745/attachment-0001.html
More information about the Tutor
mailing list