On Thu, Aug 6, 2009 at 3:42 PM, vince spicer <vinces1...@gmail.com> wrote:
> > > On Thu, Aug 6, 2009 at 3:18 PM, chase pettet <chase...@gmail.com> wrote: > >> I am trying to write a script to work our LVS implementation. I want to >> be able to have user do something like this "./script SITE SERVER" and have >> the script look up the ip value of the site on that server and issue the >> command to pull the status from LVS. I am almost there but for some reason >> when I try to pass the site parameter dynamically (the name of the >> dictionary) I keep getting errors about value type. I have two example that >> work where the dictionary name is hard coded to to speak in the script, and >> the value I want to pull is dynamic using sys.argv[1], and one where I'm >> trying to pass the dictionary name and it does not work. I tried to slim >> thse examples down, so hopefully they are helpful: >> >> works #1... >> >> ./script.py t >> >> #!/usr/bin/env python >> site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4", >> "s2":"10.1.1.5", "s3":"10.1.1.6"} >> >> def runBash(cmd): >> p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) >> out = p.stdout.read().strip() >> return out >> >> class LVS_Site: >> def show(self, site): >> showsite = "ipvsadm -L -t %s:443" % (site) >> showsiteresult = runBash(showsite) >> return showsiteresult >> >> a = LVS_Site() >> b = site["%s" % (sys.argv[1])] >> c = a.show(b) >> print "" >> print "" >> print "" >> print c >> >> works #2... >> >> ./script.py t >> >> #!/usr/bin/env python >> site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4", >> "s2":"10.1.1.5", "s3":"10.1.1.6"} >> >> def runBash(cmd): >> p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) >> out = p.stdout.read().strip() >> return out >> >> class LVS_Site: >> def show(self, site): >> showsite = "ipvsadm -L -t %s:443" % (site) >> showsiteresult = runBash(showsite) >> return showsiteresult >> >> a = LVS_Site() >> z = site >> b = z["%s" % (sys.argv[1])] >> c = a.show(b) >> print "" >> print "" >> print "" >> print c >> >> >> Not working... >> >> ./script.py t site >> >> #!/usr/bin/env python >> site = {"l":"10.1.1.1", "t":"10.1.1.2", "x":"10.1.1.3", "s1":"10.1.1.4", >> "s2":"10.1.1.5", "s3":"10.1.1.6"} >> >> def runBash(cmd): >> p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) >> out = p.stdout.read().strip() >> return out >> >> class LVS_Site: >> def show(self, site): >> showsite = "ipvsadm -L -t %s:443" % (site) >> showsiteresult = runBash(showsite) >> return showsiteresult >> >> a = LVS_Site() >> z = sys.argv[2] >> b = b["%s" % (sys.argv[1])] >> c = a.show(b) >> print "" >> print "" >> print "" >> print c >> >> Error: >> >> Traceback (most recent call last): >> File "./python2.py", line 22, in ? >> b = z["%s" % (sys.argv[1])] >> TypeError: string indices must be integers >> >> >> I don't understand why the third one does not work. Thanks for any help! >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > In your code > > z = sys.argv[2] which is a string > > think you want > > b = site[z] > or even better b = site.get(z, None) if b is None: print "Not Found"
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor