[Linux-Biella] Python, questo sconosciuto

Paolo Ciarrocchi paolo.ciarrocchi a gmail.com
Mer 22 Lug 2009 17:15:06 CEST


Spero di non essere OT nel chiedere qualche dritta sull'uso di python.

Ho scritto (grazie Daniele :-) qualche riga di codice che, una volta
ottenuto un url come argomento, ne ottiene il suo contenuto
sequenzialmente per dieci volte riportando ad output quanto tempo ha
impiegato per eseguire sia ogni singola operazione sia il totale.

Il codice (grezzo prototipo, gioco con il python da poche ore):

---
import re, urllib2, sys, BaseHTTPServer, time

def parseAddress(input):
        if input[:7] != "http://":
                if input.find("://") != -1:
                        print "Error: Cannot retrive URL, address must be HTTP"
                        sys.exit(1)
                else:
                        input = "http://" + input

        return input

def retrieveWebPage(address):
        try:
                web_handle = urllib2.urlopen(address)
        except urllib2.HTTPError, e:
                error_desc =
BaseHTTPServer.BaseHTTPRequestHandler.responses[e.code][0]
                #print "Cannot retrieve URL: " + str(e.code) + ": " + error_desc
                print "Cannot retrieve URL: HTTP Error Code", e.code
                sys.exit(1)
        except urllib2.URLError, e:
                print "Cannot retrieve URL: " + e.reason[1]
                sys.exit(1)
        except:
                print "Cannot retrieve URL: unknown error"
                sys.exit(1)
        return web_handle

if len(sys.argv) < 2:
        print "Usage:"
        print "%s url" % (sys.argv[0])
        sys.exit(1)

match_set = set()

address = parseAddress(sys.argv[1])
total = 0

for x in range (10):
	start = time.clock()
	website_handle = retrieveWebPage(address)
	end = time.clock()
	delta = end - start
	print x+1, "Time elapsed = ", delta, "seconds"
	total = total + delta
print "Elaboration completed, time elapsed = ", total
---

il problema e' che la variabile total al termine delle dieci GET e'
valorizzata a 0.03 secondi, valore che mi sembra troppo basso per
essere vero ;-/
Dove sto sbagliando?

Ciao,
-- 
Paolo


Maggiori informazioni sulla lista Linux