Monday, October 22, 2018

how to get files off coursera jupyter notebook

The Coursera VMs dont allow you to install ssh or scp for security reasons. You can email files off the website in python.


import smtplib

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart

import os


def mail_stuff():
    """
    mail stuff to gmail account cause you can't do ssh or scp from coursera
    """
    user_id="xxx"
    password = "xxx"
 
    outer = MIMEMultipart()
    outer['Subject'] = 'files from coursera'
    outer['To'] = "dougchang25@gmail.com"
    outer['From'] = "dougchang25@gmail.com"
    outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
    attachments = ['images/classification_kiank.png']
    for file in attachments:
        try:
            with open(file, 'rb') as fp:
                msg = MIMEBase('application', "octet-stream")
                msg.set_payload(fp.read())
            encoders.encode_base64(msg)
            msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
            outer.attach(msg)
        except:
            print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
            raise

    composed = outer.as_string()

 
 
    try:
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(user_id, password)
        server.sendmail(user_id, user_id, composed)
        server.close()
        print("sent!!!!")
    except:
        print ('Something went wrong...')
     
mail_stuff()

1 comment: