Notes and Assignments

2017-06-12 Final Homework

posted Jun 12, 2017, 6:06 AM by Samuel Konstantinovich

Final Homework:
REQUIRED Final Project Exit Survey:

2017-06-08 MatPlotLib is easy!

posted Jun 8, 2017, 7:23 AM by Samuel Konstantinovich   [ updated Jun 8, 2017, 8:39 AM ]

Not many people are complaining, but I am certain that some are struggling with MatPlotLib.

I found this as the second search result on google (search query was: matplotlib web server )


2017-06-01

posted Jun 1, 2017, 6:49 AM by Samuel Konstantinovich   [ updated Jun 2, 2017, 8:19 AM ]

Goal:  Hidden form data, sanitized input.

Requriements and Deadlines: 

1. Make an HTML file readme.html (link to the main site from here)
You MUST describe the working parts of your program, and how to use it here.
This will be updated as you get more things working.

2. BOTH members of the group must have ALL files in their home directories. 

3. All files must be in four subdirectories of
 ~/public_html/finalproject/

4. OPTIONALLY You can share data files like images by making a folder outside of the 3 versions
~/public_html/finalproject/data/

 
June 5/9/12.

This should be your current 'being worked on' version of the website:
~/public_html/finalproject/current
 (you can still have a data directory inside of current)

By Monday MORNING, June 5th, save a copy here:  (and check that it works as expected without errors)
~/public_html/finalproject/version1

By Friday MORNING, June 9th, save a copy here:  (and check that it works as expected without errors)
~/public_html/finalproject/version2

By Monday MORNING, June 12th, save a copy here:  (and check that it works as expected without errors)
~/public_html/finalproject/version3
Version 3 is the final version.


1. Passing old form info through the next form (to stay logged in when you submit a form)
You can use a hidden input in any form, to send info such as the MagicNumber, email address or other data.
<input type="hidden" value="foo" name="user_id" />
like:
print '<input type="hidden" name="MagicNumber"  value="'+variable_in_your_program+'"/>'

2. Clean up user input text! You do not want a user to input HTML accidentally or on purpose:

import  cgi
input = "</td></tr></table>"
output = cgi.escape(input)
print output

Result: (HTML values for the tag symbols)
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

What could happen if you did not use the cgi.escape() command on the input?



2017-05-30 User management and passwords

posted May 30, 2017, 7:14 AM by Samuel Konstantinovich   [ updated Jun 1, 2017, 8:46 AM ]

Goals:
Writing to files
handling passwords
Handling users and data


or


Creating an account
createAccount.py

#!/usr/bin/python
print 'content-type: text/html\n'
import cgi
import cgitb
import os
import hashlib
import random
cgitb.enable()


#DO NOT let people create accounts, this is for the site creators
#to use to give accounts to people they want.

#make a subdirectory called data
#in a terminal cd into the project directory and: 
#chmod 777 data
#create accounts
#chmod 700 data

#Alternatively, use filezilla.winSCP to check "W" for write,
#or use the Ubuntu file Manager right click the folder:
#properties -> allow everyone "Read and Write"

#get the fieldStorage
form = cgi.FieldStorage()
print form,"<hr>"

def main():

    #check for all the requirements
    requiredFields = ['newUserEmail','newUserPassword']
    allHere = True
    for req in requiredFields:
            allHere = allHere and req in form

    #only create an account if you have all the required data
    if not allHere:
        print '''<h1>DO NOT let people create accounts</h1>
            <p> this is for the site creators to use to give accounts to people they want.</p><hr>
            <form method="POST" action="createAccount.py">
            <h1>create an account:</h1>
            New User Email:<input type="text" name="newUserEmail">
            New User Password:<input type="password" name="newUserPassword">
            <input type="submit">
            </form>
            '''
        return

    #here you know all the requiremnets are met.

    email = form['newUserEmail'].value
    #pick a name based on a unique identifier like email.
    filename = './data/'+email+'.txt'

    #check if file exists
    if os.path.isfile(filename):
        #return when you find a duplicate file
        print "Not allowed to create a second user with the same email."
        return

    #now create an list of strings
    #populate this with the lines of your data file
    writeThis = []

    #add a new line of data for each field.
    #since the required fields are in a list, their order is preserved.
    for key in requiredFields:
            value = form[key].value
            
            if key!="newUserPassword":
                    #if it isn't a password just add the value
                    #you can expand this section 
                    #print "Writing '"+value+"'<br>"
                    writeThis.append(value)
            else:
                    #passwords are special treat them differently
                    #make a hashlib object
                    hasher = hashlib.new('sha256')
                    #add your password to it
                    hasher.update(value)
                    #digest it (turn it into a hash)
                    hash = hasher.hexdigest()
                    #print "The hash is <b>"+hash+"</b><br>"
                    writeThis.append(str(hash))
    #append a random number
    writeThis.append(str(random.randint(10000,1000000)))
    #append a fake IP
    writeThis.append("0.0.0.0");

    #join the list with new lines so we can write it to the file
    writeThis = "\n".join(writeThis)
    #print "about to write:\n"+writeThis+"<br>\n"

    #open, write, and close the output file
    outfile = open(filename,'w')
    outfile.write(writeThis)
    outfile.close()
    print "File written and closed"


main()



Login Page (can be put into the verify page, and self direct:

<form action="verifyPassword.py" method="POST">
Email:<input type="text" name="UserEmail">
Password:<input type="password" name="UserPassword">
<input type="submit" value="login">
</form>



COMPARING PASSWORDS:

verifyPassword.py


#!/usr/bin/python
print 'content-type: text/html\n'
import cgi
import cgitb
import hashlib
cgitb.enable()
import random
import os
import os.path


form = cgi.FieldStorage()

requiredFields = ['UserEmail','UserPassword']
allHere = True
for req in requiredFields:
	allHere = allHere and req in form
print allHere

if allHere:
    email = form['UserEmail'].value
    filename = "./data/"+email+".txt"
    if os.path.isfile(filename):
        password = form['UserPassword'].value
        userInfo = open(filename).read().split("\n")
        storedHash = userInfo[1]

        hasher = hashlib.new('sha256')
        hasher.update(password)
        inputHash = hasher.hexdigest()
        
        if(storedHash == inputHash):
            #print "Same passwords!<br>"
            #this is where you put information you want to hide behind a login.
            magicNumber = random.randint(100000,1000000)
            userInfo[2] = str(magicNumber)
            IP = os.environ["REMOTE_ADDR"]
            userInfo[3] = str(IP)
            outText = "\n".join(userInfo)
            outFile = open(filename,'w')
            outFile.write(outText)
            outFile.close()
            print '''You have successfully authenticated. <br> Please <a href="mainpage.py?UserEmail='''+email+'''&magicNumber='''+str(magicNumber)+'''"> click here </a>to go to the site!'''
        else:
            print "<b>Invalid</b> password.<br>"
    else:
        print "No account data found"
main()



General Pages that require being logged in:

mainpage.py

#!/usr/bin/python
print 'content-type: text/html\n'
import cgi
import cgitb
import hashlib
cgitb.enable()
import random
import os
import os.path


#THIS IS A TEMPLATE TO STAY LOGGED IN BETWEEN PAGES

def mainPage(email,magicNumber):
    #note that ALL links to new pages must include this after the .py 
    cgistuff = "?UserEmail=" + email + "&magicNumber=" + str(magicNumber)
    #see example below:
    return '''  <h1>YOUR PAGE HERE</h1><br>
    ADD CONTENT HERE<br>
    <hr>
    example of a link to some other page that checks login: <br>
    <a href="otherpage.py'''+ cgistuff+ '''"> click here </a>to go to the other page!'''

def main():
    form = cgi.FieldStorage()

    #list of all required fieldstorage keys
    requiredFields = ['UserEmail','magicNumber']
    allHere = True
    for req in requiredFields:
            allHere = allHere and req in form

    #check for required fields
    if not allHere:
        print "Not logged in properly (not all form elements)."
        return

    #get the name of the data file
    email = form['UserEmail'].value
    filename = "./data/"+email+".txt"

    #check if file exists
    if not os.path.isfile(filename):
        print "Not logged in properly (bad username)."
        return

    #open file for magic number and IP
    userInfo = open(filename).read().split("\n")
    storedMagicNumber = userInfo[2]
    storedIP = userInfo[3]
    
    
    #check magic number
    magicNumber = form['magicNumber'].value
    if storedMagicNumber != magicNumber:
        print "Not logged in properly (badMagicNumber)."
        return

    #check IP address
    IP = os.environ["REMOTE_ADDR"]
    if str(IP)!=storedIP:
        print "Not logged in properly (invalid IP)."
        return                
    

    #You got here? Great! Show the real page.
    print mainPage(email,magicNumber)            
main()


otherpage.py (just change the link, otherwise it is the same)


#!/usr/bin/python
print 'content-type: text/html\n'
import cgi
import cgitb
import hashlib
cgitb.enable()
import random
import os
import os.path


#THIS IS A TEMPLATE TO STAY LOGGED IN BETWEEN PAGES

def mainPage(email,magicNumber):
    #note that ALL links to new pages must include this after the .py 
    cgistuff = "?UserEmail=" + email + "&magicNumber=" + str(magicNumber)
    #see example below:
    return '''  <h1>YOUR PAGE HERE</h1><br>
    ADD CONTENT HERE<br>
    example of a link to some other page that checks login: <br>
    <a href="mainpage.py'''+ cgistuff+ '''"> click here </a>to go to the other page!'''

def main():
    form = cgi.FieldStorage()

    #list of all required fieldstorage keys
    requiredFields = ['UserEmail','magicNumber']
    allHere = True
    for req in requiredFields:
            allHere = allHere and req in form

    #check for required fields
    if not allHere:
        print "Not logged in properly (not all form elements)."
        return

    #get the name of the data file
    email = form['UserEmail'].value
    filename = "./data/"+email+".txt"

    #check if file exists
    if not os.path.isfile(filename):
        print "Not logged in properly (bad username)."
        return

    #open file for magic number and IP
    userInfo = open(filename).read().split("\n")
    storedMagicNumber = userInfo[2]
    storedIP = userInfo[3]
    
    
    #check magic number
    magicNumber = form['magicNumber'].value
    if storedMagicNumber != magicNumber:
        print "Not logged in properly (badMagicNumber)."
        return

    #check IP address
    IP = os.environ["REMOTE_ADDR"]
    if str(IP)!=storedIP:
        print "Not logged in properly (invalid IP)."
        return                
    

    #You got here? Great! Show the real page.
    print mainPage(email,magicNumber)        
main()




2017-05-26 HW!

posted May 26, 2017, 8:19 PM by Samuel Konstantinovich

(I described this in class but forgot to post the full description)
Deadline: Wed May 31st.
You and your partner are submitting 1 prototype of your website. This will be a part of your final project grade. Do not blow this off.
0. Give an overview and a description of features.  
    -This should be printed out
1. Draw all of the pages on your site 
    -This can be hand drawn [neatly!] or printed.
    -For pages that have dynamic content, show the format, and a couple of examples
2. Label the pictures:
    -Draw arrows, and labels
    -D them to describe how they link together. 
    -If your diagrams are hand-drawn, you may wish to print out labels and descriptions to attach.

Of course, you are allowed to do this entirely digitally. Use some web tool to help you make this without hand drawing, and label directly on that.

2017-05-24 HW Final Projects.

posted May 24, 2017, 7:07 AM by Samuel Konstantinovich

Final Projects: (HOMEWORK!)
1. Pairs of students, select a partner and have a few ideas by Friday. Print out a list of your ideas and have 1 copy per group to hand in. 
2. Projects will be websites that will work with data on some level.

Project categories:
1. Data analysis/visualization 
-Get data from any public source, save the data files, have your site read through the data.
-Users will make selections (using forms) to filter, sort, and make calculations on the data.
-Website will display results, and/or graphs (research matplotlib), and possibly conclusions.

Ideas:
-Users can select which data to view
-select two sets of data to compare.
-compare graphically, or mathematically.
-users can select how to view results, or how much of the results to see. 

2. Users have accounts and the website will store some information about each user.
-give accounts to people you trust. Do not allow account creation!
-passwords must be stored as a hash (research: hashlib)
-have them test your site

some features you can implement:
-Users make posts
-Users can comment on / up or down vote posts. 
-Users can take quizzes, and the scores are all posted and ranked. 
-Users can send messages to eachother
-Users can rate movies/music/something. 

3. Some combination of both, would be a lot of work. 

2017-05-23

posted May 23, 2017, 3:04 PM by Samuel Konstantinovich

Tuesday: 803 (physics lab)
Wednesday: 451 (regular lab)
Thursday: 803 
Friday: 451 for the rest of the semester.


2017-05-19 Friday Assignment!

posted May 18, 2017, 6:45 AM by Samuel Konstantinovich   [ updated May 18, 2017, 6:55 AM ]

Check out the cgi/form demo here:
http://homer.stuy.edu/~konstans/cgidemos/form.html
OR

There is also a python file, and the text file with the source of the py file. 

Your goals:
1. Read through the lab, and the source file, to understand how things work.
2. Fix your lab 05 and 06
  /05/wordcountCGI.py
  /06/form.html   + the py file you linked it to.
3. Complete the lab (also posted on the html file linked above)

LAB+HW

Use your newfound skills to write a madlibs form/program. You will take a paragraph or two from a book you like, and allow the user to submit words to see the results. This will ultimately be due at a later date, but starting now and asking about the problems will make it easier, especially when I add a few more parts to the HW next week.

  1. Make a new directory ~/public_html/07madlibs ( place all of the files for this lab in that directory )
  2. Take a short story (posted on the course site) save it as a text file story.txt
  3. Replace 5-10 words with easy to replace strings like $word1$ or *noun1* etc.
  4. Now make a form madlibs.py, that allows the user to submit 5-10 text boxes
  5. Label the box with the type of word you are replacing. like: noun, verb( an example of tense would be helpful), adverb, adjective, etc.
  6. Add a submit button!
  7. Write the program that responds to this form, by reading the story file, and replacing the appropriate words.
  8. Make sure the program prints the new story!


Some story excerpts:

Call me Ishmael. Some years ago- never mind how long precisely- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off- then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball.

----

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe: 
All mimsy were the borogoves, 
And the mome raths outgrabe.

"Beware the Jabberwock, my son! 
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun 
The frumious Bandersnatch!" 

He took his vorpal sword in hand; 
Long time the manxome foe he sought-
So rested he by the Tumtum tree 
And stood awhile in thought. 

----

It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way--in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.


2017-05-18 Robot Lab!

posted May 18, 2017, 6:33 AM by Samuel Konstantinovich

Goal: Write some programs to test the robot.

Tools:
1. You will need to build delays into your program. To do this we use the time
import time
#now you can use the sleep(seconds command)
time.sleep(1)

2. Loading functions from a package:
from package import *

#if you do this:
from time import *
#you can just say:
sleep(1)

The robots have a package gopigo. You do NOT have to copy this file to your directory, however you can look at its contents in order to see how the functions work.

#to use the robot commands you need to:
from gopigo import *

3. Setup your home directory:
a. Connect to your robot (use VNC)
b. Open a terminal on your robot.
c. Increase the terminal font size (edit -> preferences -> style -> terminal font size)
c. Make your home directory: (replace per04 with per05 if you are period 5)

Type the following in the terminal to make your home directory, and make it easy to write to. 
[The sudo command gives an error about resolving host, this is not a problem at all!]

cd ~/Desktop/GoPiGo/Software/Python/Stuy/
sudo mkdir skon-2017-per04  
sudo chown pi skon-2017-per4

4. Make a new python file   robotTest1.py  and try to do the following:
a. Import the gopigo, and time functions.
b. Try to make the robot turn right , wait 1 second, then turn left.
c. Do the same thing with forward and back.
d. Try to move the robot. Wait, adjust the speed, then move again. 
e. Use the speed adjustments to help you with the following:
    Turn 90 degrees, slow the robot down until it actually is close to 90 degrees. 
    Turn 90 degrees 4 times. Does the robot end up where it started?
    Try to make the robot move in a square (not too large) How accurate is it?
    Try to increase the accuracy of your square!
f. Make a function leg(n,a) that makes the robot move n centimeters, and turn a degrees.
   Use this to try to draw a triangle, and a square. 



2017-05-16 Robots

posted May 16, 2017, 7:16 AM by Samuel Konstantinovich

Reference here:
http://bert.stuy.edu/pbrooks/spring2017/materials/intro-pilot-2/programming_the_gopigo_robot.html

1-10 of 61