[Tutor] Tutor Digest, Vol 134, Issue 89

Juanald Reagan jon.engle at gmail.com
Sun Apr 26 05:02:56 CEST 2015


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',
'referral': None, 'nets': [{'updated': '2012-02-24T00:00:00', 'handle':
'NET-8-0-0-0-1', 'description': 'Level 3 Communications, Inc.',
'tech_emails': 'ipaddressing at level3.com', 'abuse_emails': 'abuse at level3.com',
'postal_code': '80021', 'address': '1025 Eldorado Blvd.', 'cidr': '8.0.0.0/8',
'city': 'Broomfield', 'name': 'LVLT-ORG-8-8', 'created':
'1992-12-01T00:00:00', 'country': 'US', 'state': 'CO', 'range': '8.0.0.0 -
8.255.255.255', 'misc_emails': None}, {'updated': '2014-03-14T00:00:00',
'handle': 'NET-8-8-8-0-1', 'description': 'Google Inc.', 'tech_emails': '
arin-contact at google.com', 'abuse_emails': 'arin-contact at google.com',
'postal_code': '94043', 'address': '1600 Amphitheatre Parkway', 'cidr': '
8.8.8.0/24', 'city': 'Mountain View', 'name': 'LVLT-GOGL-8-8-8', 'created':
'2014-03-14T00:00:00', 'country': 'US', 'state': 'CA', 'range': None,
'misc_emails': None}], 'asn': '15169'}

I would like to refine this information and only receive these fields as my
output:
{'asn_registry': 'arin',description': 'Google Inc.',}

I was hoping there was a way to take each one of these items and read them
into a list so I can pick and choose the fields I want by index. I believe
all fields are currently stored in index position 0.

Here are the helper file contents:

 |      Args:

 |          inc_raw: Boolean for whether to include the raw whois results in

 |              the returned dictionary.

 |          retry_count: The number of times to retry in case socket errors,

 |              timeouts, connection resets, etc. are encountered.

 |          get_referral: Boolean for whether to retrieve referral whois

 |              information, if available.

 |          extra_blacklist: A list of blacklisted whois servers in
addition to

 |              the global BLACKLIST.

 |          ignore_referral_errors: Boolean for whether to ignore and
continue

 |              when an exception is encountered on referral whois lookups.

 |

 |      Returns:

 |          Dictionary: A dictionary containing the following keys:

 |                  query (String) - The IP address.

 |                  asn (String) - The Autonomous System Number.

 |                  asn_date (String) - The ASN Allocation date.

 |                  asn_registry (String) - The assigned ASN registry.

 |                  asn_cidr (String) - The assigned ASN CIDR.

 |                  asn_country_code (String) - The assigned ASN country
code.

 |                  nets (List) - Dictionaries containing network
information

 |                      which consists of the fields listed in the NIC_WHOIS

 |                      dictionary. Certain IPs have more granular network

 |                      listings, hence the need for a list object.

 |                  raw (String) - Raw whois results if the inc_raw
parameter

 |                      is True.

 |                  referral (Dictionary) - Dictionary containing referral

 |                      whois information if get_referral is True and the

 |                      server isn't blacklisted. Consists of fields listed

 |                      in the RWHOIS dictionary. Additional referral server

 |                      informaion is added in the server and port keys.

 |                  raw_referral (String) - Raw referral whois results if
the

 |                      inc_raw parameter is True.

 |

 |  *lookup_rws*(self, inc_raw=False, retry_count=3)

 |      The function for retrieving and parsing whois information for an IP

 |      address via HTTP (Whois-RWS).

 |

 |      NOTE: This should be faster than IPWhois.lookup(), but may not be as

 |          reliable. AFRINIC does not have a Whois-RWS service yet. We have

 |          to rely on the Ripe RWS service, which does not contain all of
the

 |          data we need. LACNIC RWS is in beta v2.

 |

 |      Args:

 |          inc_raw: Boolean for whether to include the raw whois results in

 |              the returned dictionary.

 |          retry_count: The number of times to retry in case socket errors,

 |              timeouts, connection resets, etc. are encountered.

 |

 |      Returns:

 |          Dictionary: A dictionary containing the following keys:

 |                  query (String) - The IP address.

 |                  asn (String) - The Autonomous System Number.

 |                  asn_date (String) - The ASN Allocation date.

 |                  asn_registry (String) - The assigned ASN registry.

 |                  asn_cidr (String) - The assigned ASN CIDR.

 |                  asn_country_code (String) - The assigned ASN country
code.

 |                  nets (List) - Dictionaries containing network
information

 |                      which consists of the fields listed in the NIC_WHOIS

 |                      dictionary. Certain IPs have more granular network

 |                      listings, hence the need for a list object.

 |                  raw (Dictionary) - Whois results in Json format if the

 |                      inc_raw parameter is True.


On Sat, Apr 25, 2015 at 8:43 PM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. Re: whois github package (Alan Gauld)
>    2. Looking up a value in a dictionary (Danny Yoo)
>    3. REPL format (Jim Mooney)
>    4. Re: REPL format (Danny Yoo)
>    5. Re: REPL format (Danny Yoo)
>    6. Re: REPL format (Alan Gauld)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 26 Apr 2015 00:25:59 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] whois github package
> Message-ID: <mhh7q4$9r8$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
>
> On 25/04/15 14:46, Juanald Reagan wrote:
> > Hello! I have a question regarding how to use/implement a package found
> at
> > github.
> >
> > https://github.com/secynic/ipwhois
>
>
> We are not experts on this here since this list s for people learning
> the core Python lamguage and its standard library. Howe er I see Steven
> has had a go at helping you.
>
> Another thing you could try is using Pythons built in help() function:
>
> After
> >>> from ipwhois import IPWhois
>
> Try
>  >>> help(IPWhois)
>
> >      obj = IPWhois(ipaddy)
>
> Now try
>
>  >>> help(obj)
>  >>> help(obj.lookup)
>
> That might give some clues about what methods exist and what they
> return. (Assuming the author included doc strings of course!
>
> >      results = [obj.lookup()]
> >      print results [0]
> >
> > This returns ALL the fields not just the "asn_registry" field.
>
> That's what I'd expect since you put the output of lookup()
> as the first element of the list. You then printed that element.
>
> But if you showed us even a snippet of what that actually
> looked like it would help us answer your question about
> how to access it. Remember we don't know this package so
> you have to show us what is happening not just summarize
> what you think its doing.
>
>  > I looked for documentation on github
>
> Python documentation is often embedded in the module/objects
> help() will often reveal it.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 25 Apr 2015 16:38:15 -0700
> From: Danny Yoo <dyoo at hashcollision.org>
> To: Juanald Reagan <jon.engle at gmail.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: [Tutor] Looking up a value in a dictionary
> Message-ID:
>         <
> CAGZAPF4VBGQRSvvKFrtpayLxh8pcM85zYdLDwSreuDAzPXD5-g at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Sat, Apr 25, 2015 at 11:13 AM, Juanald Reagan <jon.engle at gmail.com>
> wrote:
> > Okay, so it doesn't look like that worked...here is the traceback. I
> don't
> > understand the second part of your request.
> >
> > Jons-desktop:whois-0.7 2 jon$ python pythonwhois.py
> >
> > 8.8.8.8
> >
> > Traceback (most recent call last):
> >
> >   File "pythonwhois.py", line 14, in <module>
> >
> >     print results.asn_registry
> >
> > AttributeError: 'dict' object has no attribute 'asn_registry'
>
>
> Because of the lack of subject-line context, I'm having a hard time
> following this thread.  I've changed the subject to "Looking up
> attributes in a dictionary."  Trying to trace the context by looking
> through the archive... ok, I see:
>
>     https://mail.python.org/pipermail/tutor/2015-April/105122.html
>     https://mail.python.org/pipermail/tutor/2015-April/105123.html
>
> ... got it....
>
>
> Ok, so you're replying back to Stephen, about the code snippet he
> proposed, and you're running into an error.  I think I understand
> better now.
>
>
> Please try changing:
>
>     print results.asn_registry
>
> to the two statements:
>
>     print results.keys()
>     print results['asn_registry']
>
>
> The first statement shows what keys are in the results dictionary.
> The second statement tries to print the value associated with
> 'asn_registry'.
>
> The reason that the original suggestion didn't work here is because,
> in Python, object attribute lookup is different from dictionary
> lookup.  We guessed that 'results' was an object, and that you wanted
> to look up the 'asn_registry' attribute of that object.
>
> But from the error message, we see that 'results' is a dictionary.
> Easy to correct.  You want to use a dictionary lookup instead.
>
> The first print statement is there just to validate that there is such
> a key 'asn_registry' within the 'results'.  It's possible that the key
> is different, so the first print is there just as a double-check for
> us.
>
>
> ------------------------------
>
> Message: 3
> Date: Sat, 25 Apr 2015 16:38:33 -0700
> From: Jim Mooney <cybervigilante at gmail.com>
> To: "tutor at python.org" <tutor at python.org>
> Subject: [Tutor] REPL format
> Message-ID:
>         <CALRAYNVz-Vz5gZk0_oArfqaGRPsCe3-ctOocPs=
> Y9yJBwbWeSA at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> I'm curious why, when I read and decode a binary file from the net in one
> fell swoop, the REPL prints it between parentheses, line by line but with
> no commas, like a defective tuple. I can see breaking lines visually, at
> \n, but if the parentheses don't mean anything I can't see including them.
> Or do they mean something I've missed? Also, it's interesting that although
> HTML is case-free, you have to get the case right for the java server page.
> getbusesforroute.jsp doesn't work.
>
> ?import urllib.request
> u = urllib.request.urlopen('
> http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')
> data = u.read()
> f = open('rt22.xml', 'wb')
> f.write(data)
> f.close()
>
> f = open('rt22.xml', 'rb')
> transit_info = str(f.read(), encoding='utf-8')
>
> >>> transit_info
> ('<?xml version="1.0"?>\r\n'
>  '\r\n'
>  '\r\n'
>  ... bunch of stuff ...
>  '\t\t\t<wid2>222</wid2>            \t\r\n'
>  '\t\t</bus>\r\n'
>  '\r\n'
>  '\r\n'
>  '\r\n'
>  '\t</buses>\r\n')
>
>
>
> --
> Jim
>
> If you only had one hour left to live, would you spend it on Facebook,
> Twitter, or Google Plus?
>
>
> ------------------------------
>
> Message: 4
> Date: Sat, 25 Apr 2015 17:38:58 -0700
> From: Danny Yoo <dyoo at hashcollision.org>
> To: Jim Mooney <cybervigilante at gmail.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] REPL format
> Message-ID:
>         <
> CAGZAPF4PpHP6cGCnm3UwhbjdB8u75kba5BoyYTe1hWcxk8aAGg at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Sat, Apr 25, 2015 at 4:38 PM, Jim Mooney <cybervigilante at gmail.com>
> wrote:
> > I'm curious why, when I read and decode a binary file from the net in one
> > fell swoop, the REPL prints it between parentheses, line by line but with
> > no commas, like a defective tuple.
>
>
> The REPL is trying to be nice here.  What you're seeing is a
> representation that's using a little-known Python syntactic feature:
> string literals can be spread across lines.  See:
>
>
> https://docs.python.org/2/reference/lexical_analysis.html#string-literal-concatenation
>
> At the program's parse time, the Python compiler will join adjacent
> string literals automatically.
>
>
> It's a cute-but-nasty trick that some other languages do, such as C++.
>
>
> I would strongly discourage not using it yourself in your own
> programs: it's the source of a very common mistake.  Here's an
> example:
>
> ####################
> def f(x, y):
>     print(x)
>     print(y)
>
> f("hello"
>   "world")
> ####################
>
> What do you expect to see?  What do you see?
>
> So that's why I don't like this feature: makes it really hard to catch
> mistakes when one is passing string literals as arguments and forgets
> the comma.  Especially nasty when the function being called uses
> optional arguments.
>
>
> But I'm surprised, frankly, that you're seeing the standard Python
> REPL using string literal concatenation, as in my memory, it was a lot
> less accommodating.  Is this Python 3?
>
>
> ------------------------------
>
> Message: 5
> Date: Sat, 25 Apr 2015 17:42:15 -0700
> From: Danny Yoo <dyoo at hashcollision.org>
> To: Jim Mooney <cybervigilante at gmail.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] REPL format
> Message-ID:
>         <CAGZAPF5qHN3yeQuDFa74HD0cYO8S7fK64Wz=D31bX2=
> FWeESEw at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> > I would strongly discourage not using it yourself in your own
> > programs.
>
> Ugh.  There was one too many negations there.  I deserved to make that
> mistake, since my sentence structure was unnecessarily nested.  :P
>
> I meant to say: "I would strongly discourage using literal string
> concatenation in your own programs: it's the source of a very common
> mistake..."
>
>
> ------------------------------
>
> Message: 6
> Date: Sun, 26 Apr 2015 01:43:23 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] REPL format
> Message-ID: <mhhcb8$7vr$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
>
> On 26/04/15 00:38, Jim Mooney wrote:
>
> > ...it's interesting that although
> > HTML is case-free, you have to get the case right for the java server
> page.
> > getbusesforroute.jsp doesn't work.
>
> That's because its a file name and has nothing to do with HTML.
> The HTML is what's inside the file.
>
> > ?import urllib.request
> > u = urllib.request.urlopen('
> > http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')
> > data = u.read()
>
> What do you get if you print data here?
> That will tell you whether its something to do with that you
> get from the server or whether its to do with what you are
> doing to the data when you save/read it.
>
> > f = open('rt22.xml', 'wb')
> > f.write(data)
> > f.close()
> >
> > f = open('rt22.xml', 'rb')
> > transit_info = str(f.read(), encoding='utf-8')
> >
> >>>> transit_info
> > ('<?xml version="1.0"?>\r\n'
> >   '\r\n'
> >   '\r\n'
> >   ... bunch of stuff ...
> >   '\t\t\t<wid2>222</wid2>            \t\r\n'
> >   '\t\t</bus>\r\n'
>
> I can't reproduce this format in the interpreter so I don't
> know what's going on. I seem to recall you are using Python 3,
> is that correct?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 134, Issue 89
> **************************************
>



-- 
Cheers,

   Jon S. Engle
   jon.engle at gmail.com


More information about the Tutor mailing list