views:

245

answers:

4

I want to get an HTML page with python and then print out all the IPs from it. I will define an IP as the following:

x.x.x.x:y

Where: x = a number between 0 and 256. y = a number with < 7 digits.

Thanks.

+1  A: 

The basic approach would be:

  • Use urllib2 to download the contents of the page
  • Use a regular expression to extract IPv4-like addresses
  • Validate each match according to the numeric constraints on each octet
  • Print out the list of matches

Please provide a clearer indication of what specific part you are having trouble with, along with evidence to show what it is you've tried thus far.

Rob
Right. The only part I cant do is the regular expression one.
If someone shows me that, I will be fine.
A: 

Try:

re.compile("\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d:\d+").findall(urllib2.urlopen(url).read())

Matthew Wilkes
Needs to be coupled with something to constrain the acceptable octet values.
Rob
+3  A: 

Right. The only part I cant do is the regular expression one. – das 9 mins ago If someone shows me that, I will be fine. – das 8 mins ago

import re

ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b")
junk = " 1.1.1.1:123 2.2.2.2:321 312.123.1.12:123 "
print ip.findall(junk)

# outputs ['1.1.1.1:123', '2.2.2.2:321']

Here is a complete example:

import re, urllib2

f = urllib2.urlopen("http://www.samair.ru/proxy/ip-address-01.htm")
junk = f.read()

ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b")
print ip.findall(junk)

# ['114.30.47.10:80', '118.228.148.83:80', '119.70.40.101:8080', '12.47.164.114:8888', '121.
# 17.161.114:3128', '122.152.183.103:80', '122.224.171.91:3128', '123.234.32.27:8080', '124.
# 107.85.115:80', '124.247.222.66:6588', '125.76.228.201:808', '128.112.139.75:3128', '128.2
# 08.004.197:3128', '128.233.252.11:3124', '128.233.252.12:3124']
Unknown
Gross! But well done none the less. Is that tested?
Ross
Yes it is. Try it yourself.
Unknown
In [9]: ip.search("255x255x255x255:12")Out[9]: <_sre.SRE_Match object at 0x103c650>
llimllib
@ llimllib: >>> ip.match("255x255x255x255:12")>>>
Unknown
well now you escaped your periods and it works. nice work!
llimllib
A: 

Not to turn this into a who's-a-better-regex-author-war but...

(\d{1,3}\.){3}\d{1,3}\:\d{1,6}
Ross
You can't have IPs with zeroes?
llimllib
Needs to be coupled with something to constrain the acceptable octet values.
Rob
"\d{1,3}" simply means 1-3 digits. A couple already submitted more "correct" regexes already, so it's a moot point.
Ross
wow, retarded comment on my part. I apologize.
llimllib