Reputation: 11
ive tried with regex like this, but i just get [] as the output
import socket
import re
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
while True:
data = str(s.recvfrom(65565))
pattern = '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'
ips = re.findall(pattern, data)
print(ips)
Upvotes: 1
Views: 52
Reputation: 43169
Three-four improvements:
(?:...)
) instead of capturing groups for parts of the IP addressre.findall()
only yields results for captured groups$
), is this intended? If so, the last \b
is meaninglessAll of these combined, you could possibly use
pattern = r'\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.|$)){4})'
# ---^---
And you should be fine. See a working demo on regex101.com.
Upvotes: 1