title: Using nmap library in Python program
body: <p>I have a simple Python program which uses nmap library to do port scanning.</p>
  
  <pre><code>from optparse import OptionParser
  import nmap  
  from threading import * 
  
  screenLock=Semaphore(value=1)
  
  def nmapScan(thost,tport):
      x=nmap.PortScanner()
      x.scan(thost,tport)
      state=x[thost]['tcp'][int(tport)]['state']
      print "[*]" + thost + "tcp/"+tport+" "+state
  
  
  def main():
      parser=OptionParser('usage %prog -H &lt;target host&gt; -p &lt;target port&gt;')
      parser.add_option('-H',dest='thost',type='string',help='specify target host')
      parser.add_option('-p',dest='tports',type='string',help='specify target port[s] seperated by comma')
      (options,args)=parser.parse_args()
      thost=options.thost
      tports=options.tports
      tports=tports.split(',')
      if (thost==None)|(tports==None):
          print parser.usage
          exit(0)
      for i in tports:
          nmapScan(thost,i)
  
  main()
  </code></pre>
  
  <p>When i run the program, i get the following error.</p>
  
  <pre><code>akshayrajmacbookpro$ python nmapScanner.py -H 192.168.1.60 -p 80,443
  Traceback (most recent call last):
    File "nmapScanner.py", line 28, in &lt;module&gt;
  main()
  File "nmapScanner.py", line 26, in main
  nmapScan(thost,i)
  File "nmapScanner.py", line 10, in nmapScan
  state=x[thost]['tcp'][int(tport)]['state']
  File "build/bdist.macosx-10.11-intel/egg/nmap/nmap.py", line 555, in __getitem__
  KeyError: '192.168.1.60'
  </code></pre>
  
  <p>I tried using url instead of ip in the command line. But I get the same error. Being new to Python, I am not able to understand and resolve this.</p>