Home > Blockchain >  Using a list element to retrieve a value from e result object
Using a list element to retrieve a value from e result object

Time:03-06

I'm new to Python. I'm trying to get a subset of whois info for a list of IP addresses.

These statements worked:

01  oWhois = IPWhois(sIP)
02  resWhoisInfo = oWhois.lookup_whois()
03  sWhois_CIDR = resWhoisInfo["nets"][0]['cidr']
04  sWhois_Name = resWhoisInfo["nets"][0]['name']
0N  ...

Until I got a result that was incomplete and I got an index error.

So rather than putting an if/else statement around lines 03, 04, 0N, I thought I'd be clever and define the following:

  nWhoisCIDR = 0
  nWhoisName = 1
  nWhoisCountry = 2
  nWhois... = N

  lWhoisItems=[[nWhoisCIDR, 'cidr'],[nWhoisName, 'name'],[nWhoisCountry, 'country'],[nWhois..., 'whatever']]

So that I could use a loop and reference items like this:

  for i in lWhoisItems:
    try:
      lWhoisResults = lWhoisResults   resWhoisInfo["nets"][0][(lWhoisItems[i][1])]
    except IndexError:
      lWhoisResults = lWhoisResults   "no value found"

But when I try to that I get an error: "TypeError: list indices must be integers or slices, not list" - even though (lWhoisItems[0][1]) == 'cidr'... (and so on).

So my question, how can I make this work? Or is this style of programming just not very Pythonish? If not could you please point me to (an example of) a solution that pretty much does the same?

Thank you very much!!!

CodePudding user response:

You were almost right dude you just missed this detail. If you print i in the for loop by adding this line here.

  for i in lWhoisItems:
    print(i)
    try:
      lWhoisResults = lWhoisResults   resWhoisInfo["nets"][0][(lWhoisItems[i][1])]
    except IndexError:
      lWhoisResults = lWhoisResults   "no value found"

You will get the output something like this

[0, 'cidr']
[1, 'name']
[2, 'country']
...

So you can use unpacking to get the index you need.

  for i, key in lWhoisItems:
    try:
      lWhoisResults = lWhoisResults   resWhoisInfo["nets"][0][lWhoisItems[i]]
    except IndexError:
      lWhoisResults = lWhoisResults   "no value found"

You can do even better by accessing the key itself.

  for i, key in lWhoisItems:
    try:
      lWhoisResults = lWhoisResults   resWhoisInfo["nets"][0][key]
    except IndexError:
      lWhoisResults = lWhoisResults   "no value found"

CodePudding user response:

I have two questions.

  1. How should your end result look like?
  2. Have you tried looking at the data inside the object? How does it look.

Let me try to show you what I have experimented.

In [12]: oWhois = IPWhois('8.8.8.8')

In [13]: resWhoisInfo = oWhois.lookup_whois()

In [14]: nets = resWhoisInfo["nets"]

In [15]: nets
Out[15]: 
[{'cidr': '8.0.0.0/9',
  'name': 'LVLT-ORG-8-8',
  'handle': 'NET-8-0-0-0-1',
  'range': '8.0.0.0 - 8.127.255.255',
  'description': 'Level 3 Parent, LLC',
  'country': 'US',
  'state': 'LA',
  'city': 'Monroe',
  'address': '100 CenturyLink Drive',
  'postal_code': '71203',
  'emails': ['[email protected]',
   '[email protected]',
   '[email protected]'],
  'created': '1992-12-01',
  'updated': '2018-04-23'},
 {'cidr': '8.8.8.0/24',
  'name': 'LVLT-GOGL-8-8-8',
  'handle': 'NET-8-8-8-0-1',
  'range': None,
  'description': 'Google LLC',
  'country': 'US',
  'state': 'CA',
  'city': 'Mountain View',
  'address': '1600 Amphitheatre Parkway',
  'postal_code': '94043',
  'emails': ['[email protected]', '[email protected]'],
  'created': '2014-03-14',
  'updated': '2014-03-14'}]

Let me know how the final result should look like. I will try to help you.

  • Related