f0rd42
f0rd42

Reputation: 1459

data from url open in list

I want to download a file from the net, i.e.: http://www.malwaredomainlist.com/hostslist/ip.txt and put that in a list to further manipulate the items in the list.

I tried

print "Downloading with urllib2"
    f = urllib2.urlopen(malwareurl)
    data = [f.read()]
    result = [stuff.replace("\r\n", "/32,") for stuff in data]
    print result
    print len([result])

the list itself "looks" fine:

['100.42.50.110/32,103.14.120.121/32,.......']

but the length is only 1.
I think I need to loop through the readlines and create items in the list for each readline, correct?

Upvotes: 0

Views: 756

Answers (3)

Sheshananda Naidu
Sheshananda Naidu

Reputation: 1025

list1 = []
print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read()
for ip in ips:
   ip = ip.strip()
   ip = ip + "/32"
   list1.append(ip)
print list1
print len(list1)

Upvotes: 0

Anandhakumar R
Anandhakumar R

Reputation: 391

>>> r=urllib2.urlopen('http://www.malwaredomainlist.com/hostslist/ip.txt')
>>> f=open('e.txt','w')
>>> ff=r.read()
>>> f.write(ff)
>>> f.close()

Upvotes: 0

I think you're overcomplicating it:

print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read().split("\r\n")

# If you want to add '/32' to each IP
ips = [x + "/32" for x in ips if x]

Upvotes: 2

Related Questions