danke
Code: Alles auswählen
class DummyFile:
def __init__(self, text):
self.text = text
def write(self, text):
self.text += text
def read(self):
return self.text
def readlines(self):
return self.text.splitlines()
class open:
def __init__(self, arg, user=None, pwd=None):
self.text = ""
port = None
if "://" in arg:
proto, rest = arg.split("://", 1)
machine, path = rest.split("/", 1)
if ":" in path:
z = path.split(":")
port = z[-1]
path = ":".join(z[:-1])
else:
proto = "file"
path = rest
z = path.split("/")
file = z[-1]
path = "/".join(z[:-1])
print [proto, machine, path, file, port]
if proto == "file":#==============================FILE
f = file(path + file)
def read():
return f.read()
def save():
f.close()
f = file(path + file)
def close():
f.close()
elif proto == "ftp":#=============================FTP
from ftblib import FTP
f.cwd(path)
f = FTP(machine)
if user and pwd:
f.login(user, pwd)
def read():
f.retrbinary("RETR " + file, lambda(text): text)
return text
def save():
f.storbinary(path + file, DummyFile(self.text)) #oder doch 'storlines'?
return DummyFile.read()
def close():
f.close()
elif proto == "http":#============================HTTP
from urllib import urlopen
def read():
f = urlopen(arg)
return f.read()
def close():
f.close()
def save():
raise kaWas, "save not supported for HTTP"#ok, es gibt ne HTTP-put methode aber....
self.save = save
self.read = read
self.close = close
def __del__(self):
self.close()
f = open("http://www.google.de/test/foo.html")
print f.read()