Hashing

What is a hash?
H(x) -> y

ex. crc32 – checksums
md5 – fast
sha1 – secure
sha256 -pretty good

set-cookie:visit = 5, [hash]

making a hash

import hashlib

def hash_str(s):
    return hashlib.md5(s).hexdigest()

def make_secure_val(s):
    return "%s, %s" % (s, hash_string(s))

checking correct hash

def check_secure_val(h):
    val = h.split('.')[0]
    if h == make_secure_val(val):
        return val
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        visits = 0
        visits = self.request.cookies.get('visits')
        if visit_cookie_val:
            cookie_val = check_secure_val(visit_cookie_str)
            if cookie_val:
                visits = ini(cookie_val)
        
        visits += 1

HMAC is hash-based message authentication code
hmac(secret, key, h)-> [HASH]

$ hmac.new(“secret”, “hoge”).hexdigest()

import hashlib
import hmac

SECRET = 'imsosecret'
def hash_str(s):
    return hmac.new(SECRET, s).hexdigest()

def make_secure_val(s):
    return "%s|%s" % (s, hash_str(s)) 

def check_secure_val(h):
    val = h.split('|')[0]
    if h == make_secure_val(val):
        return val

database should change password hashing
random function in python

def make_salt():
    return ''.(random.choice(string.letters) for x in xrange(5))
def make_pw_hash(name, pw):
    salt = make_salt()
    h = hashlib.sha256(name + pw * salt).hexdigest()
    return '%s,%s' % (h, salt)

Python Set-Cookie

Set-Cookie: name=steve; Domain=www.rddit.com; Path=/

domain is restricted wwww.

Third party set cookie such as google analytics
ad network also set cookie

Set-Cookie: user=123; Expire= Ture, 1 Jan
"session" cookie = no Expire

session cookie delete when close the browser.

def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        visits = self.request.cookies.get('visits', 0)
        if visits.isdigit();
            visits = int(visits) + 1
        else:
            visits = 0
        self.response.headers.add_header('Set-Cookie', 'visits=%s' % visits)
        if visits > 100:
            self.write("you are the best ever")
        else:
            self.write("you've been here %s times!" % visits)

write document.cookie in console.
we can rewrite cookie.

document.cookie
"wordpress_test_cookie=WP+Cookie+check; wp-settings-1=editor%3Dhtml%26libraryContent%3Dbrowse%26imgsize%3Dmedium; wp-settings-time-1=1482994300; _ga=GA1.2.511761152.1479929467"
document.cookie="wp-settings-tims-1=1482994301"
"wp-settings-tims-1=1482994301"

	

db connection

db. = sqlite3.connect(':memory:')
db.execute('create table links ' +
            '(id integer, submitter_id integer, submitted_time integer, ' +
            'votes integer, title text, url text)')
for l in links:
    db.execute('insert into links values (?, ?, ?, ?, ?, ?)', l)

def query():
    c = db.execute("select * from links")
def query():
    cursor = db.execute("select * from links")
    for link_tuple in cursor:
        link = Link(*link_tuple)

print query()
def query():
    cursor = db.execute("select * from links where submitter_id = 62443 and votes > 1000")
    link = Link(*c.fetchone())
    return link.id

order by

def query():
    cursor = db.execute("select * from links where submitter_id = 62443 order by submitter_time asc")
    for link_tuple in c:
        link = Link(*link_tuple)
        results.append(link.id)
    return results

join

def link_by_id(link_id):
    for l in links:
        if l.id == link_id:
            return l

scaling database is to 1.replicate, 2.shared.

Querying

from collections import namedtuple

# make a basic Link class
Link = namedtuple('Link', ['id', 'submitter_id', 'submitted_time', 'votes',
                           'title', 'url'])

# list of Links to work with
links = [
    Link(0, 60398, 1334014208.0, 109,
         "C overtakes Java as the No. 1 programming language in the TIOBE index.",
         "http://pixelstech.net/article/index.php?id=1333969280"),
    Link(1, 60254, 1333962645.0, 891,
         "This explains why technical books are all ridiculously thick and overpriced",
         "http://prog21.dadgum.com/65.html"),
    Link(23, 62945, 1333894106.0, 351,
         "Learn Haskell Fast and Hard",
         "http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/"),
    Link(2, 6084, 1333996166.0, 81,
         "Announcing Yesod 1.0- a robust, developer friendly, high performance web framework for Haskell",
         "http://www.yesodweb.com/blog/2012/04/announcing-yesod-1-0"),
    Link(3, 30305, 1333968061.0, 270,
         "TIL about the Lisp Curse",
         "http://www.winestockwebdesign.com/Essays/Lisp_Curse.html"),
    Link(4, 59008, 1334016506.0, 19,
         "The Downfall of Imperative Programming. Functional Programming and the Multicore Revolution",
         "http://fpcomplete.com/the-downfall-of-imperative-programming/"),
    Link(5, 8712, 1333993676.0, 26,
         "Open Source - Twitter Stock Market Game - ",
         "http://www.twitstreet.com/"),
    Link(6, 48626, 1333975127.0, 63,
         "First look: Qt 5 makes JavaScript a first-class citizen for app development",
         "http://arstechnica.com/business/news/2012/04/an-in-depth-look-at-qt-5-making-javascript-a-first-class-citizen-for-native-cross-platform-developme.ars"),
    Link(7, 30172, 1334017294.0, 5,
         "Benchmark of Dictionary Structures", "http://lh3lh3.users.sourceforge.net/udb.shtml"),
    Link(8, 678, 1334014446.0, 7,
         "If It's Not on Prod, It Doesn't Count: The Value of Frequent Releases",
         "http://bits.shutterstock.com/?p=165"),
    Link(9, 29168, 1334006443.0, 18,
         "Language proposal: dave",
         "http://davelang.github.com/"),
    Link(17, 48626, 1334020271.0, 1,
         "LispNYC and EmacsNYC meetup Tuesday Night: Large Scale Development with Elisp ",
         "http://www.meetup.com/LispNYC/events/47373722/"),
    Link(101, 62443, 1334018620.0, 4,
         "research!rsc: Zip Files All The Way Down",
         "http://research.swtch.com/zip"),
    Link(12, 10262, 1334018169.0, 5,
         "The Tyranny of the Diff",
         "http://michaelfeathers.typepad.com/michael_feathers_blog/2012/04/the-tyranny-of-the-diff.html"),
    Link(13, 20831, 1333996529.0, 14,
         "Understanding NIO.2 File Channels in Java 7",
         "http://java.dzone.com/articles/understanding-nio2-file"),
    Link(15, 62443, 1333900877.0, 1244,
         "Why vector icons don't work",
         "http://www.pushing-pixels.org/2011/11/04/about-those-vector-icons.html"),
    Link(14, 30650, 1334013659.0, 3,
         "Python - Getting Data Into Graphite - Code Examples",
         "http://coreygoldberg.blogspot.com/2012/04/python-getting-data-into-graphite-code.html"),
    Link(16, 15330, 1333985877.0, 9,
         "Mozilla: The Web as the Platform and The Kilimanjaro Event",
         "https://groups.google.com/forum/?fromgroups#!topic/mozilla.dev.planning/Y9v46wFeejA"),
    Link(18, 62443, 1333939389.0, 104,
         "github is making me feel stupid(er)",
         "http://www.serpentine.com/blog/2012/04/08/github-is-making-me-feel-stupider/"),
    Link(19, 6937, 1333949857.0, 39,
         "BitC Retrospective: The Issues with Type Classes",
         "http://www.bitc-lang.org/pipermail/bitc-dev/2012-April/003315.html"),
    Link(20, 51067, 1333974585.0, 14,
         "Object Oriented C: Class-like Structures",
         "http://cecilsunkure.blogspot.com/2012/04/object-oriented-c-class-like-structures.html"),
    Link(10, 23944, 1333943632.0, 188,
         "The LOVE game framework version 0.8.0 has been released - with GLSL shader support!",
         "https://love2d.org/forums/viewtopic.php?f=3&t=8750"),
    Link(22, 39191, 1334005674.0, 11,
         "An open letter to language designers: Please kill your sacred cows. (megarant)",
         "http://joshondesign.com/2012/03/09/open-letter-language-designers"),
    Link(21, 3777, 1333996565.0, 2,
         "Developers guide to Garage48 hackatron",
         "http://martingryner.com/developers-guide-to-garage48-hackatron/"),
    Link(24, 48626, 1333934004.0, 17,
         "An R programmer looks at Julia",
         "http://www.r-bloggers.com/an-r-programmer-looks-at-julia/")]


# links is a list of Link objects. Links have a handful of properties. For
# example, a Link's number of votes can be accessed by link.votes if "link" is a
# Link.

# make the function query() return the number of votes for the link whose ID is
# 15

def query():
    submissions = []
    for l in links:
        if submitter_id = 62443:
            submissions.append(l)
    submissions.sort(key = lambda x: x.submitted_time)
    return submissions
print query()

template

import os
import webapp2

form_html = """
<form>
<h2>Add a Food</h2>
<input type="text" name="food">
%s 
<button>Add</button>
</form>
"""
hidden_html = """
<input type="hidden" name="food" value="%s">
"""

shopping_list_html = """
<br>
<br>
<h2>Shopping List</h2>
<ul>
%s
</ul>
"""

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)

class MainPage(Handler):
    def get(self):
        output = form_html
        hidden_html = ""

        items = self.request.get_all("food")
        if items:
        output_items = ""
        for item in items:
            output_hidden += hidden_html % item
            output_items += item_html % item

        output_shopping = shopping_list_html % output_items
        output += output_shopping

        output = output % output_hidden
            
        self.write(output)

app = webapp2.WSGIApplication([('/', MainPage),
                    ],
                    debug=True)
application:template-lesson
version: 1
runtime: python27
api_version: 1
threadsafe: True

handlers:
  - url: /.*
    script: templates.app

python template

<!DOCTYPE html>

<html>
  <head>
    <title>templates!</title>
  </head>

  <body style="margin: 0">
    <h1 style="background-color: #ddd: color: #888; margin: 0, height: 50px">
      Templates
    </h1>
    {% block content %}
    {% endblock %}
  </body>
</html>
{% extends "base.html" %}

{% block content %}
<form>
      <h2>Add a Food</h2>
      <input type="text" name="food">
      {% if items %}
        {% for item in items %}
          <input type="hidden" name="food" value="{{item}}">
        {% endfor %}
      <button>Add</button>

      {% if items %}
      <br>
      <br>

      <h2>Shopping List</h2>
      <ul>
        {% for item in items %}
        <li>{{ item | escape }}</li>
      </ul>
      {% endif %}
</form>
{% endblock %}

rot13

def _rot13(c):
if ‘A’ <= c and c <= 'Z': return chr((ord(c) - ord('A') + 13) % 26 + ord('A')) if 'a' <= c and c <= 'Z': return chr((ord(c) - ord('a') + 13) % 26 + ord('a')) return c
def render_str(template, **params):
	t = jinja_env.get_template(template)
	return t.render(params)

class BaseHandler(webapp2.RequestHandler):
	def render(self, template, **kw):
		self.response.out.write(render_str(template, **kw))

	def write(self, *a, **kw):
		self.response.out.write(*a, **kw)

class Rot13(BaseHandler):
	def get(self):
		self.render('rot13-form.html')

	def post(self)
		rot13 = ''
		text = self.request.get('text')
		if text:
			rot13 = text.encode('rot13')

		self.render('rot13-form.html', text = rot13)

USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
	return username and USER_RE.match(username)

PASS_RE = re.compile(r"^.{3,20}$")
def valid_password(password):

python %s

replaced by given string.

given_string = "I think %s is a perfectly normal thing to do in public."
def sub1(s):
     return given_string % s
given_string2 = "I think %s and %s are perfectly normal things to do in public."
def sub2(s1, s2):
    return given_string2 % (s1, s2)

string substitution

given_string2 = "I'm %(nickname)s. My real name is %(name)s, but my friends call me %(nickname)s."
def sub_m(name, nickname):
    return given_string2 % {"nickname": nickname, "name" : name}
    
print sub_m("Mike", "Goose") 

replacement

def escape_html(s):
   for (i, o) in (("&", "&amp;"),
                (">", "&gt;"),
                ("<", "&lt;"),
                ('"', "&quote;")):
        s = s.replace(i, o)
    return s

 print escape_html('>')
import cgi
def escape_html(s):
   return cgi.escape(s, quote = True)

 print escape_html('"hello, & = &amp;"')

validation

str.capitalize(): returns a copy of the string with only its first character capitalized.

months = ['January',
          'February',
          'March',
          'April',
          'May',
          'June',
          'July',
          'August',
          'September',
          'October',
          'November',
          'December']
          
def valid_month(month):
    if month:
        cap_month = month.capitalize()
        if cap_month in months:
            return cap_month

use dictionary to restrict only three character.

month_abbvs = dic((m[:3].lower(), m) for m in months)
          
def valid_month(month):
    if month:
        short_month = month[:3].lower()
        return month_abbvs.get(short_month)

checking day

def valid_day(day):
    if day and day.isdigit():
     day = int(day)
     if day > 0 and <= 31:
         return day

telnet

[vagrant@localhost]$ sudo yum -y install telnet
Trying 35.160.185.106...
Connected to www.udacity.com.
Escape character is '^]'.
GET / HTTP/1.0
Host: www.example.com

HTTP/1.1 302 Found
Cache-Control: no-cache
Location: https://www.example.com/
Content-Length: 0
Connection: Close

input form

<form action="http://www.google.com/search">
  <input name="q">
  <input type="submit">
</form>

play.py

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

app = webapp2.WSGIApplication([('/', MainPage)],
    debug=True)

ダブルクォーテーション3つでくくると、複数行に渡る文字列を記述することができる。

import webapp2

form="""
<form action="http://www.google.com/search">
  <input name="q">
  <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):
    def get(self):
        #self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

app = webapp2.WSGIApplication([('/', MainPage)],
    debug=True)

form parameter

<form>
  <input type="radio" name="q" value="one">
  <input type="radio" name="q" value="two">
  <input type="radio" name="q" value="third">
  <br>
  <input type="submit">
</form>
<form>
    <label>
        one
        <input typ\e="radio" name="q" value="one">
    </label>
    <label>
        two
        <input type="radio" name="q" value="two">
    </label>
    <label>
        three
        <input type="radio" name="q" value="third">
    </label>
  <br>
  <input type="submit">
</form>

select

<form>
    <select name="q">
        <option value="1">the number one</option>
        <option>two</option>
        <option>three</option>
    </select>
  <br>
  <input type="submit">
</form>

User validation

import webapp2

form="""
<form method="post">
  What is your birthday?
  <br>
  <label>Month
  <input type="text" name="month">
  </label>
  <label>Day
  <input type="text" name="day">
  </label>
  <label>Year
  <input type="text" name="year">
  </label>

  <br>
  <br>
  <input type="submit">
</form>
"""

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)

    def post(self):
        self.response.out.write("Thanks! That's a totally valid day!")

app = webapp2.WSGIApplication([('/', MainPage),('/testform', TestHandler)],
    debug=True)

gulp sass

npm install gulp-sass

var gulp = require('gulp');
gulp.task('default', function(){
  console.log("hello, gulp!");
});

gulp.task('styles', function(){
  gulp.src('sass/**/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./css'));
});

gulp.task('default', function(){
  gulp.watch('sass/**/*.scss',['styles']);
});

Linting is the process of running a program that will analyse code for potential errors.
ESLint

[vagrant@localhost rss24]$ sudo npm install -g eslint

span:inline
div:block

parameter
?name=value

fragment
#fragment