syntactic sugar for filesystem access in Python

Adam Sampson ats1 at ukc.ac.uk
Fri Sep 29 13:06:17 CEST 2000


"Delaney, Timothy" <tdelaney at avaya.com> writes:

> 	sock = internet.192.168.0.1['http']

How about:

#!/usr/bin/python

import socket

class InternetServer:
	"""A class representing a site on the Internet."""
	address = None
	def __init__(self, address):
		self.address = address
	def __getitem__(self, item):
		if item == "tcp":
			return InternetTCPServer(self)
		else:
			raise IndexError
	def __getattr__(self, attr):
		return InternetServer(attr + "." + self.address)

class InternetProtocolServer:
	"""A class representing a protocol that can be served by an
	Internet site."""
	server = None
	def __init__(self, server):
		self.server = server

class InternetTCPServer(InternetProtocolServer):
	def __getitem__(self, key):
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.connect((self.server.address, key))
		return s

class Internet(InternetServer):
	"""A class representing the Internet."""
	def __init__(self):
		InternetServer.__init__(self, "")

internet = Internet()

if __name__ == "__main__":
	s = internet.org.python.www["tcp"][80]
	s.send("GET / HTTP/1.0\n\n")
	print s.recv(10000) # bleurgh

-- 

Adam Sampson
azz at gnu.org



More information about the Python-list mailing list