0

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?

fredtantini
  • 15,966
  • 8
  • 49
  • 55
f0rd42
  • 1,429
  • 4
  • 19
  • 30

3 Answers3

2

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]
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
0
>>> r=urllib2.urlopen('http://www.malwaredomainlist.com/hostslist/ip.txt')
>>> f=open('e.txt','w')
>>> ff=r.read()
>>> f.write(ff)
>>> f.close()
Anandhakumar R
  • 371
  • 1
  • 3
  • 17
0
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)
Sheshananda Naidu
  • 905
  • 1
  • 9
  • 10