Skip to content

Commit

Permalink
New Module ftp.py and PEP 8 for ftp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Jendryke committed Dec 5, 2017
1 parent deb1679 commit c31569e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ftp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pycurl
from io import BytesIO

def dirlist(u):
# lets create a buffer in which we will write the output
buffer = BytesIO()

# lets create a pycurl object
c = pycurl.Curl()

# lets specify the details of FTP server
# c.setopt(pycurl.URL, r'ftp://ftp.ncbi.nih.gov/refseq/release/')
c.setopt(pycurl.URL, u)

# lets assign this buffer to pycurl object
c.setopt(pycurl.WRITEFUNCTION, buffer.write)

# lets perform the LIST operation
try:
c.perform()
except:
code = c.getinfo(pycurl.HTTP_CODE)
print('ERROR: cURL HTTP_CODE:', code)
c.close()

# lets get the buffer in a string
body = buffer.getvalue()
print(body)
return body # Returns Bytes!!!


def file(u, o):
with open(o, 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, u)
c.setopt(c.WRITEDATA, f)
c.setopt(c.NOPROGRESS, 1) # Show (0) or not show (1) the progress
print('INFO: Downloading file from {u} to {o}'.format(o=o, u=u))
try:
c.perform()
except:
code = c.getinfo(pycurl.HTTP_CODE)
print('ERROR: cURL HTTP_CODE:', code)
c.close()

0 comments on commit c31569e

Please sign in to comment.