[Tutor] Looking up a value in a dictionary from user input problem

vince spicer vinces1979 at gmail.com
Thu Aug 6 23:42:02 CEST 2009


On Thu, Aug 6, 2009 at 3:18 PM, chase pettet <chase.mp at 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 at 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]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090806/1b236d2d/attachment.htm>


More information about the Tutor mailing list