[Tutor] print issue

ose micah osaosemwe at yahoo.com
Fri Oct 4 12:04:40 EDT 2019


 Thanks Alan,
I have solved the problemm, main issue, I think was I was not importing json and using the items in the dictionary properly:here is the solution:



import requests

import json

resp = requests.get('http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips')

ip_json = json.loads(resp.text)

for eachSubnet in ip_json['CLOUDFRONT_GLOBAL_IP_LIST']:

     print eachSubnet


Thanks 
Ose M.
    On Thursday, October 3, 2019, 07:12:49 PM EDT, Alan Gauld <alan.gauld at btinternet.com> wrote:  
 
 On 03/10/2019 22:02, ose micah wrote:

> cloudfront_ips = [item['ip_prefix'] for item in ip_ranges if item ==
> "CLOUDFRONT_GLOBAL_IP_LIST"]

This line makes no sense.

First of all you treat item as a dictionary using item['ip_prefix']
Then you treat item as a string with item=="CLOUDFRONT_GLOBAL_IP_LIST"

It can't be both, its either a dict or a string. Which is it?



> ec2_ips = [item['ip_prefix'] for item in ip_ranges if item ==
> "CLOUDFRONT_REGIONAL_EDGE_IP_LIST"]

And this is the same.

> for ip in cloudfront_ips:
>     if ip not in ec2_ips:
>         cloudfront_ips_more_edge.append(ip)

Why not use list comprehensions here too:

cloudfront_ips_more_edge=[ip for ip in cloudfront_ips
                            if ip not in ec2_ips]

An alternative approach would be to use sets and
use the intersection and other set operators

> for ip in ec2_ips:
>     if ip not in cloudfront_ips:
>         cloudfront_ips_more_edge.append(ip)

same as above...

> for ip in cloudfront_ips_more_edge: print(str(ip))

I suspect ip is already a string so you shouldn't need the str() conversion.


-- 
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

  


More information about the Tutor mailing list