Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Hit Counter?

  1. #1
    Regular Coder
    Join Date
    Sep 2005
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hit Counter?

    Hello all Python programmers,
    I am new to python, although I have read lots and learnt lots considering the amount of time that I have been trying.

    I was wondering if it is possible to create a web counter in python. I was just looking through the things like:

    Code:
    f = open("somefile.txt")
    f = write('Some text that this will write')
    f = close
    and other functions alike.



    Is it possible to do something like this PHP code:

    Code:
    <?php 
    //Open data 
    $datafile = file("hits.txt"); 
    
    // Find out how many hits 
    $hits = datafile[0]; 
    
    // Add one 
    $hits++; 
    
    // File handle 
    $filehandle = fopen("$datafile", 'w'); 
    
    // Write new hit number 
    fwrite($filehandle, $hits); 
    
    // Save file 
    fclose($filehandle); 
    
    ?>
    hits.txt

    Code:
    0

    in python.


    Any help would be greatly appreciated.


    Alex.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Saving a hit counter in a file is the worst solution. There will be concurrency problems without very good solutions.

    The correct approach is to store the hits in a database.

    Create the hit_log table:
    Code:
    create table hit_log (hit_time timestamp default current_timestamp);
    The hit adder:
    Code:
    import psycopg2 as db
    
    connection = db.connect('host=localhost dbname=test user=user password=password')
    cursor = connection.cursor()
    
    query = 'insert into hit_log default values'
    cursor.execute(query)
    
    cursor.close()
    connection.commit()
    connection.close()
    To query the hit_log table:
    Code:
    select count(*) from hit_log where hit_time::date = '2006-12-11'::date;
    The samples use the postgresql database. Translate it to your database.

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, I will use that.

    But is there a way to do it with just a textfile?



    Oh yeah... I'm having trouble integrating the python with html, would you be able to help me with that one?



    Cheers ,
    Alex.


    EDIT:

    This is the code that I came up with last night, it works fine (I think) but I dont know how to put it in HTML.

    Code:
    myfile = open("hits.txt", "r")
    hits = myfile.read()
    myfile.close()
    hitsint = int(hits) + 1
    newhits = str(hitsint)
    myfile = open("hits.txt", "w")
    myfile.write(newhits)
    myfile.close()

    Thanks again,
    Alex
    Last edited by Mr. Bubble; 12-12-2006 at 12:04 AM. Reason: Forgot to add another bit


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •