I have found that specifying the proto in the lport param enabled the for bundled loop to correctly see the strings within the dict. Below is the correct script that allows for the python-nmap parser to work correctly. Obviously the for bundle only caters for TCP, however another params with for bundle would suffice for the UDP requirement.
import nmap # import nmap.py module
nm = nmap.PortScanner()
host = '127.0.0.1'
nm.scan(host, '1-1024')
nm.command_line()
nm.scaninfo()
for host in nm.all_hosts():
print('----------------------------------------------------')
print('Host : %s (%s)' % (host, nm[host].hostname()))
print('State : %s' % nm[host].state())
print('----------------------------------------------------')
for proto in nm[host].all_protocols():
print('----------')
print('Protocol : %s' % proto)
lport = nm[host]['tcp'].keys() #<------ This 'proto' was changed from the [proto] to the ['tcp'].
lport.sort()
for port in lport:
print('----------------------------------------------------')
print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
print('----------------------------------------------------')
I'm not a python expert(yet) and had some help from a friend (Tx AdriaanDL :)). However it does sort out the problem I have been having with this sample that the nmap.py developers have on their website.