Skip to content

Instantly share code, notes, and snippets.

@betodealmeida
Created March 26, 2013 21:06
Show Gist options
  • Save betodealmeida/5249245 to your computer and use it in GitHub Desktop.
Save betodealmeida/5249245 to your computer and use it in GitHub Desktop.
import os
import urllib
from ftplib import FTP
import socket
from pupynere import netcdf_file
class FTPFile(object):
def __init__(self, url, buffer=4096):
protocol, location = url.split('://', 1)
auth, host = urllib.splituser(location)
if auth:
username, password = urllib.splitpasswd(auth)
else:
username = password = None
host, filepath = urllib.splithost('//' + host)
path, self.filename = os.path.split(filepath)
self.ftp = FTP(host, username, password)
self.ftp.set_debuglevel(2)
self.ftp.login()
self.ftp.sendcmd("TYPE i")
self.ftp.cwd(path)
self.buffer = buffer
self.position = 0
self.closed = False
def tell(self):
return self.position
def seek(self, position):
self.position = position
def read(self, count):
offset = self.position
self.position += count
# start reading from offset
s = self.ftp.transfercmd("RETR " + self.filename, offset)
try:
data = ''
while True:
chunk = s.recv(self.buffer)
if not chunk or len(data) >= count:
break
data += chunk
except socket.timeout:
raise Exception
return data[:count]
def close(self):
self.closed = True
buf = FTPFile('ftp://user:password@host/path/to/file.nc')
f = netcdf_file(buf)
print f.variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment