[Tutor] Tutor Digest, Vol 134, Issue 89
Steven D'Aprano
steve at pearwood.info
Sun Apr 26 13:22:53 CEST 2015
Hi Juanald, or Jon if you prefer,
You're still replying to the digest, which means we're still getting six
or eight pages of messages we've already seen.
My answer to your question is below:
On Sat, Apr 25, 2015 at 11:02:56PM -0400, Juanald Reagan wrote:
> Sorry for not providing all the relevant info, let me provide some
> additional details:
>
> When I run this code:
>
> from ipwhois import IPWhois
> file=open('ip.txt', 'r')
> ipaddy=file.read()
> obj = IPWhois(ipaddy)
> results = [obj.lookup()]
> print results [0]
>
> I receive this output:
>
> Jons-computer:whois-0.7 2 jon$ python pythonwhois.py
>
> {'asn_registry': 'arin', 'asn_date': '', 'asn_country_code': 'US', 'raw':
> None, 'asn_cidr': '8.8.8.0/24', 'raw_referral': None, 'query': '8.8.8.8',
[snip output]
Awesome! That's the information we needed to see. IPWhois() returns a
dictionary of {key: value} pairs. So we can extract the fields you want
like this:
obj = IPWhois(ipaddy)
registry = obj['asn_registry']
description = obj['description']
If you insist on using a list with indexed values, you can do this:
obj = IPWhois(ipaddy)
results = [obj['asn_registry'], obj['description']]
registry = results[0]
description = results[1]
but in my opinion that's silly since you already have the information in
the obj dictionary, copying it into the results list is just a waste of
time.
Note that this will *not* work:
obj = IPWhois(ipaddy)
results = list(obj.values())
registry = results[ ???? ] # What index should I use?
description = results[ ???? ]
because dictionaries are unordered, and you don't know what order the
values will be returned. Depending on the version of Python you are
using, it might even be a different order every time you run the
program.
--
Steve
More information about the Tutor
mailing list