



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS107 Handout 37 Spring 2008 May 30, 2008
Here’s the coordinate.112.4-45.8
class ContentHandler: def startDocument(self, tag): pass def endDocument(self, tag): pass def startElement(self, tag, attributes): pass def endElement(self, tag): pass def characters(self, data): pass
from urllib2 import urlopen from xml.sax import make_parser, ContentHandler import sys
class RSSNewsFeedTagHandler(ContentHandler): def init(self): ContentHandler.init(self) # equivalent of super() in Java self.__indentLevel = 0 # initially at an indentation level of 0 def startElement(self, tag, attributes): for i in xrange(self.__indentLevel): sys.stdout.write(" ") sys.stdout.write("<%s>\n" % tag) self.__indentLevel += 1 def endElement(self, tag): self.__indentLevel - = 1 for i in xrange(self.__indentLevel): sys.stdout.write(" ") sys.stdout.write("\n" % tag)
2210 Hope Lane Cinnaminson New Jersey 08077
856- 786 - 06xx 856- 829 - 81xx 856- 829 - 72xx
from urllib2 import urlopen from xml.dom.minidom import parse import sys def listTitles(feeds): for feed in feeds: instream = urlopen(feed) xmltree = parse(instream) items = xmltree.getElementsByTagName("item") for item in items: titlesubtree = item.getElementsByTagName("title") assert len(titlesubtree) >= 1, "Failed to find a title within a particular item subtree" assert len(titlesubtree) <= 1, "Unexpected found more than one title within the item" titleNode = titlesubtree[0] titleTextNode = titleNode.firstChild title = titleTextNode.nodeValue sys.stdout.write("%s\n" % title) feeds = ["http://feeds.gawker.com/wonkette/full", "http://feeds.gawker.com/gawker/full", "http://feeds.gawker.com/lifehacker/full", "http://feeds.gawker.com/gizmodo/full", "http://feeds.gawker.com/consumerist/full", "http://feeds.gawker.com/valleywag/full"] listTitles(feeds)
from socket import socket, AF_INET, SOCK_STREAM from time import gmtime, strftime from sys import argv import sys def startServer(portNum): print "Starting timestamp server (listing to port %d)" % portNum s = socket(AF_INET, SOCK_STREAM) s.bind(("", portNum)) s.listen(5) try: while True: client, address = s.accept() print "Accepted connection from remote client at " + str(address) infile = client.makefile() while True: line = infile.readline() if line == "\r\n": break sys.stdout.write(line) infile.close() client.send("Current time is: " + strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())) client.close() except KeyboardInterrupt: print "Detected stop request: Killing server"; s.close() defaultPortNumber = 40000 def getPortNumber(): port = defaultPortNumber if (len(argv) > 1): try: port = int(argv[1]) except ValueError: print "Non-numeric argument detected... using default port" return port startServer(getPortNumber())