User Profile

Collapse

Profile Sidebar

Collapse
Atrisa
Atrisa
Last Activity: Jun 5 '11, 07:19 PM
Joined: Sep 17 '10
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • Atrisa
    started a topic User-Agent browser info

    User-Agent browser info

    Hi everyone. I want to get list of browsers from the User-Agent strings in HTTP header. The browser info is said to be the second entry inside the parenthesis (U for Mozilla Firefox), but sometimes that is not enough and it must be related to other entries, like in the following where the browser is Google Chrome and seen second last in the string.

    Code:
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko)
    ...
    See more | Go to post

  • I could solve the problem by using
    Code:
    netloc
    instead of
    Code:
    hostname
    and then some manual parsing. Thanks bvdet.
    See more | Go to post

    Leave a comment:


  • I changed
    Code:
    urlparse(ref).hostname
    to
    Code:
    urlparse(ref).netloc
    , now it neither gives error nor prints anything. Is there anything else that could do what I want
    Code:
    hostname
    to do?
    See more | Go to post

    Leave a comment:


  • How to fix error "'NoneType' object has no attribute 'split'"?

    I have a file with lines like this:

    Code:
    Host: gateway.edge.messenger.live.com 
    Referer: 13.gateway.edge.messenger.live.com/help 
    Host: ads.lfstmedia.com
    Referer: http://facebook.mafiawars.com:80/?
    Host: gateway.edge.messenger.live.com 
    Referer: 13.gateway.edge.messenger.live.com/help 
    Host: ads.lfstmedia.com
    Referer: 13.gateway.edge.messenger.live.com/help
    Host: ads.google.com
    ...
    See more | Go to post

  • Atrisa
    replied to In a dictionary, sum values if similar keys
    Thanks dwblas. For some reason it kept telling me that score is not defined, though it is. After making some changes, now the working code looks like this:
    Code:
    scoreDict.setdefault(ID, 0)
    scoreDict[ID] += int(score)
    thanks a lot again,
    Atrisa
    See more | Go to post

    Leave a comment:


  • Atrisa
    started a topic In a dictionary, sum values if similar keys

    In a dictionary, sum values if similar keys

    Hi everyone. I have a file of records like this:

    ID:13
    Date: 12.12
    Scores: 2

    ID:22
    Date: 9.12
    Scores: 4

    ID:13
    Date: 18.12
    Scores: 4

    I want to have the unique IDs as keys and the sum of the socres for that ID as values in the scoreDict dictionary. This is my code so far:

    Code:
    if ID is not None:
        if ID not in scoreDict:
    ...
    See more | Go to post

  • Atrisa
    replied to How to loop through previous lines
    Thanks BV for your hints, helps and instructions:)
    See more | Go to post

    Leave a comment:


  • Atrisa
    replied to How to loop through previous lines
    Now it is working.

    Code:
    dd = {}
    for line in open('test.txt'):
        if line.startswith('Writer'):
            writer = line.split(':')[1].strip('\n')
            dd.setdefault(writer, 0)
            dd[writer] += 1
    print dd
    See more | Go to post

    Leave a comment:


  • Atrisa
    replied to How to loop through previous lines
    Still the dictionary is empty.
    See more | Go to post

    Leave a comment:


  • Atrisa
    replied to How to loop through previous lines
    This is what I did, but don't know why it doesn't print anything:

    Code:
    f = open('test.txt').read()
    
    dd = {}
    for line in f.split('\n\n'):
        if line.startswith('Writer'):
            writer = line.split(':')[0]
            dd.get(writer, 0)
            dd[writer] += 1
    print dd
    See more | Go to post

    Leave a comment:


  • Atrisa
    started a topic How to loop through previous lines

    How to loop through previous lines

    Hi everyone. I have a text file looking like this:

    Book ID: 1
    Availability: on shelf > 3 borrows > 2
    Name: Bread and Wine
    Writer: Silone

    Book ID: 2
    Availability: on shelf > 4 borrows > 1
    Name: Fontamara
    Writer: Silone

    Book ID: 3
    Availability: on shelf > 3 borrows > 2
    Name: Poirot
    Writer: Agatha Christie

    Book...
    See more | Go to post

  • Finally these are the line that did it:

    Code:
    for key in dd.keys():
        for item in ports_name.keys():
            if key == item:
                outfile.write("\n".join(["%s %s %s" % (key, dd[key], ports_name[item])])+ "\n")
    Thanks bvdet for your help.
    See more | Go to post

    Leave a comment:


  • Now I get the port description in the third column of the output file 'capture25000-column3.txt', but each description is repeated for few times, not just once as the port numbers are. Some output:

    25910 1 pangolin-laser
    55149 1 pangolin-laser
    4024 4 www-http
    13734 2 www-http
    2451 2 www-http
    55617 6 iapp
    61510 1 iapp

    which is not correct. The few last lines of the following code...
    See more | Go to post

    Leave a comment:


  • Unfortunately that didn't work either. I changed few lines in the previously sent code this way:
    Code:
    .
    .
    .
    port_table[port[0]]=line1[0]
            keycount +=1
            content=f.readline()
    
    ports_name={}
    get_port_name ('Ports', ports_name)
    and now the dictionary looks like this:
    -----------------------------------------------
    {'8732': 'dtp-net', '4026': 'as-debug',...
    See more | Go to post

    Leave a comment:


  • Now I have created the following method in the code that you did earlier. The method creates a dictionary that has the port numbers and their corresponding port names:

    Code:
    def get_port_name (name, port_table):
    # ports.txt is a file where I have put the IANA PORT NUMBERS
        f= open('ports.txt','r')
        content=f.readline()
        keycount=1
        while content:
            key = str(keycount)
    ...
    See more | Go to post

    Leave a comment:


  • Thanks a lot bvdet for your help and your time. That works perfectly. It's two weeks that I have started learning Python, that's why I am a bit lost.
    See more | Go to post

    Leave a comment:


  • I also want to count how many times the port numbers appeared in the 'column3' and put the port number and beside it the number of appearances of that port in a file, so that I have something like this:

    21283 3
    21283 2
    23479 1
    80 7
    10464 2

    and so on. This is my code that tries to do that, but it gives me only 1 appearance for each port:

    Code:
    outfile
    ...
    See more | Go to post

    Leave a comment:


  • Thanks a lot bvdet. That worked well:)
    See more | Go to post

    Leave a comment:


  • I first have an input file that has captured data. I need only the third column from that file, so I did it this way:
    Code:
    # for (i, eachline) in enumerate(open('capture25000.txt','r')):
    #  column3 = eachline.split(' ')[2]
    Then I get the list 'column3' which looks like this:

    27.20.29.12.212 83
    27.20.29.12.212 83
    65.24.112.69.23 479
    27.120.98.21.80
    32.16.45.74.104 64
    56.176.45.74.10 464...
    See more | Go to post
    Last edited by bvdet; Sep 20 '10, 06:29 PM.

    Leave a comment:


  • The actual data which is in a file called 'capture25000-column3.txt' is a set of port numbers like this:

    52141
    21283
    43439
    21283
    21283
    23479
    80
    10464
    80
    80
    21283
    43439
    80
    10464
    10464
    21283
    .
    .
    .

    and there are 25000 rows of those numbers. If you need all of them, I could send it to your email.
    See more | Go to post

    Leave a comment:

No activity results to display
Show More
Working...