Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2006, 04:25 AM   PM User | #1
Mr. Bubble
Regular Coder

 
Join Date: Sep 2005
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Mr. Bubble is an unknown quantity at this point
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.
Mr. Bubble is offline   Reply With Quote
Old 12-11-2006, 11:02 AM   PM User | #2
Kakao
Regular Coder

 
Join Date: Mar 2006
Location: Brasília, Brazil
Posts: 153
Thanks: 0
Thanked 0 Times in 0 Posts
Kakao is on a distinguished road
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.
Kakao is offline   Reply With Quote
Old 12-11-2006, 10:58 PM   PM User | #3
Mr. Bubble
Regular Coder

 
Join Date: Sep 2005
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Mr. Bubble is an unknown quantity at this point
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-11-2006 at 11:04 PM.. Reason: Forgot to add another bit
Mr. Bubble is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:47 AM.


Advertisement
Log in to turn off these ads.