manningtree

Python for the new year

I’d looked at but then avoided Python because it wasn’t a real programming language. No curly braces. No semicolons.

But intrigued by this minimalist web server:

PORT = 9914
SERVER = ’127.0.0.1′
 
import SimpleHTTPServer
import BaseHTTPServer
import SocketServer

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
    pass

httpd = Server((SERVER, PORT), Handler)
print "Web Server listening on http://%s:%s/ (stop with ctrl+c)…" % (SERVER, PORT)

try:
    httpd.serve_forever()
except KeyboardInterrupt:
    print "Going down…"

included in the processing.js examples I took another look.

Here’s a quick script to calculate a hash of The Guardian’s home page (as a simple crude way to see if it changed):

import urllib
import hashlib

pageURL = "http://www.guardian.co.uk"
f = urllib.urlopen(pageURL)
pageContent = f.read()
f.close()

m = hashlib.md5()
m.update(pageContent)
value = m.hexdigest()
print value

The best bit? Curley braces:

pagesDict = {pageURL:  value}

Going to dig some more and experiment with a view to using in the classroom alongside some C# and JavaScript we’re already using.

Recommended reading: the Python edition of How to think like a Computer Scientist. Oh, and Spike had a poem about it. Something about being curled around a tree and eaten.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>