Reputation: 185
I have a text file which have text similar to mentioned below
harry's source ip and port combination is 192.168.4.1/5897 and he is trying to access destination 202.158.14.1/7852
The text may vary. My task is to find the first pair of IP and port.
However my code is not working
import re
with open('traffic.txt', 'r') as file:
fi = file.readlines()
re_ip = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
re_port = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$\/(\d+)")
for line in fi:
ip = re.findall(re_ip,line)
port = re.findall(re_port,line)
print port , ip
Upvotes: 0
Views: 6245
Reputation: 185
Correct code
import re
with open('traffic.txt', 'r') as file:
fi = file.readlines()
re_ip = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
re_port = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(\d+)")
for line in fi:
port = re.findall(re_port,line)
ip = re.findall(re_ip,line)
print "PORT is " , port , "ip is " ,ip
Upvotes: 2