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

Discussão em 'Python' iniciado por Stack, Junho 13, 2025.

  1. Stack

    Stack Membro Participativo

    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 re

    openFile = 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] = 1
    else:
    wordfreq[word] += 1

    print(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.

    Continue reading...

Compartilhe esta Página