#! /usr/bin/env python import sys import time import random from socket import * cardinal = [ 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW' ] template = """\ HTTP/1.0 200 Content-type : text/html Simon's weather station

Today's Weather

Time: %s

Wind Speed: %s knots

Wind Direction: %s

Temperature: %s deg C

Rainfall: %s mm

""" port = 8080 def weather(): # Simulate doing something time.sleep(1) ws = random.randrange( 0, 35 ) wd = random.choice( cardinal ) t = random.randrange( -5, 35 ) r = random.randrange( 0, 100 ) now = time.ctime() return template % ( now, ws, wd, t, r ) sock = socket( AF_INET, SOCK_STREAM ) sock.bind(( '', port )) sock.listen(5) print 'Creating server:' , sock.getsockname() while 1: newsock, address = sock.accept() print 'Client connection from: ', address print 'Got: ' + newsock.recv( 1024 ) w = weather() print 'Sending: ' + w newsock.send( w ) newsock.close()