How to use python regex to extract IP address from server log files?

2024/11/8 3:07:48

I am currently getting started with python. I have a server log file for the pages I visited over a period of time.

How do I write a python program to find out which IP address was visited most? Will I have to use dictionary? I have done this but I am not sure how to use regex to fetch IP addresses.

import reopenFile = open('text.txt', "r")readLines = openFile.read()
# pat = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
wordfreq = {}for word in readLines.split():if word not in wordfreq:wordfreq[word] = 1else:wordfreq[word] += 1print(wordfreq)# wordList = [(v,k) for k,v in wordfreq.items()]
# wordList.sort(reverse=True)
# 
# print(wordList)

PS: I don't want to use counter from python module. I am figuring out to do this with dictionary.

Answer

Using Regex and Counter

Demo:

import re
from collections import Counter
s = """www.google.com : 255.111.111.111-some random stuff-www.facebook.com : 255.222.222.222-some random stuff-www.google.com : 255.111.111.111-some random stuff-www.google.com : 255.111.111.111-some random stuff-
"""ips = re.findall("www\.[A-za-z]+\.[a-z]+\s+\:\s+(.*$)", s, flags=re.MULTILINE)
print(Counter(ips).most_common(1))

Output:

[('255.111.111.111', 3)]
https://en.xdnf.cn/q/120664.html

Related Q&A

DateFormatter returning wrong dates - Matplotlib/Pandas [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions concerning problems with code youve written must describe the specific problem — and incl…

What does the error IndentationError: expected an indented block in python mean? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How to insert character in csv cell in python?

Im new with python. Here is my csv file :data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; dataThe thing that I want…

How can you initialise an instance in a Kivy screen widget

I am trying to access an instance variable named self.localId in my kivy screen and it keeps saying the saying the instance doesnt exist after i have initialised it. I know I have an error In my code b…

is a mathematical operator classed as an interger in python

in python is a mathematical operator classed as an interger. for example why isnt this code workingimport randomscore = 0 randomnumberforq = (random.randint(1,10)) randomoperator = (random.randint(0,2)…

Fix a function returning duplicates over time?

I have a function here that returns a 4 digit string. The problem is that when I run the function like 500 times or more, it starts to return duplicates. How to avoid that?My Function:import random de…

Pandas method corr() use not all features

I have dataframe with shape (335539, 26). So I have 26 features. But when i use data.corr() I get a 12 x 12 matrix.What can be wrong? `

int to datetime in Python [duplicate]

This question already has answers here:Convert string "Jun 1 2005 1:33PM" into datetime(26 answers)Parsing datetime in Python..?(2 answers)Closed 5 years ago.Im receiving data from the port.…

How to extract the historical tweets from twitter API? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

ValueError: invalid literal for int() with base 16: [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…