Προς το περιεχόμενο

Ελεγχος σε ενα python script


Nik0s

Προτεινόμενες αναρτήσεις

Δημοσ.

Ρε παιδια καποιος που ξερι Python ας τεσταρει λιγο τον παρακατω κωδικα γιατι ολο Internal server error μου παραγει.

 

>
#!/usr/bin/python

import cgi, MySQLdb
import os, sys, socket, datetime

# get some enviromental values
page = os.environ['SCRIPT_NAME']
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' )

# connect to database, get cursor, get form fields in this case url extra variable strings
conn = MySQLdb.connect( db = 'webville_tech', host = 'localhost', user = 'webville', passwd = '*****' )
cursor = conn.cursor()
form = cgi.FieldStorage()

print ( "Content-type: text/html\n" )


# if extra string is attached to the URL is 'stats' then show statistics for ALL pages and exit
# =================================================================================================================
if form.getfirst('show') == 'stats':
try:
	cursor.execute( "SELECT page, hits FROM counters ORDER BY hits DESC" )
except:
	print ( """Λάθος Εκτέλεση εντολής MySQL.""" )
else:
	results = cursor.fetachall()
		
	#display results
	print ( """<center><br><h3> [ Σελίδα ] <==========> [ Επισκέψεις ] </h3><br>""" )
		
	print ( """<table border = "1" cellpadding = "3">""" )
		
	for row in results:
		print ("<tr>")
			
		for entry in row:
			print ( "<td>%s %s</td>", results.page, results.hits )
		
	print ( """</table>""" )
	sys.exit(0)

	
# if extra string is attached to the URL is 'log' then show page log and exit
# =================================================================================================================
if form.getfirst('show') == 'log':

try:
	cursor.execute( "SELECT * FROM visits WHERE page_id = $PageID ORDER BY date DESC" )
except:
	print ( """Λάθος Εκτέλεση εντολής MySQL.""" )
else:
	results = cursor.fetachall()
		
	#display results
	print ( """<center><br><h4> [ ΕΠΙΣΚΕΠΤΗΣ ] <==========> [ ΣΥΝΟΛΟ ΕΠΙΣΚΕΨΕΩΝ ] <==========> [ ΗΜΕΡΟΜΗΝΙΑ ] </h4><br>""" )
		
	print ( """<table border = "1" cellpadding = "3">""" )
		
	for row in results:
		print ( "<tr>" )
			
		for entry in row:
			print ( "<td>%s %s %s</td>", results.host, results.hits, result.date )
		
	print ( """</table>""" )
	sys.exit(0)

	
# do not increment the counter if a Cookie is set to the visitors browser already
# =================================================================================================================
if form.getcookie('visitor') == 'nikos':
host = "nikos"
   
if host != "nikos":
# insert new page record or update it if exists
try:
	cursor.execute( "INSERT INTO counters(page_id, page, hits) VALUES($PageID, \"$page\", 1) \
					ON DUPLICATE KEY UPDATE page = \"$page\", hits = hits + 1" )
except:
	print ( """Λάθος Εκτέλεση εντολής MySQL.""" )

# if an existing record with same page_id and same host found in table visits then update entry else create a new visitor record
try:
	cursor.execute( "UPDATE visits SET hits = hits + 1, date = \"$date\" WHERE page_id = $PageID AND host = \"$host\" " )
except:
	print ( """Λάθος Εκτέλεση εντολής MySQL.""" )
else:
	results = cursor.fetachall()
	if results.rowcount:
		cursor.execute( "INSERT INTO visits(page_id, host, hits, date) VALUES($PageID, \"$host\", 1, \"$date\") ")


# return general counter, render the template and print it
# =================================================================================================================
cursor.execute( "SELECT hits FROM counters WHERE page_id = %s", page )
counter = cursor.fetchone()

if counter.rowcount:
data = template.data % { 'counter': counter }
print ( data )
else:
print ( """Μηδεν! Δεν βάζεις κάτι πιο πικάντικο αφεντικό να τραβήξουμε το ενδιαφέρον?!""" )

 

Eυχαριστω πολυ.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...