cis192 python programmingcis192/html/files/lec/week10/lec11.pdfwhat servers do when a client makes a...

24
CIS192 Python Programming Web Frameworks and Web APIs Harry Smith University of Pennsylvania November 8, 2017 Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 1 / 24

Upload: others

Post on 26-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

CIS192 Python ProgrammingWeb Frameworks and Web APIs

Harry Smith

University of Pennsylvania

November 8, 2017

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 1 / 24

Page 2: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Outline

1 Web ServersWhat’s a Web Server?Flask

2 Web APIsRESTEncoding and Encryption

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 2 / 24

Page 3: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Web Servers

We’ve been talking about making HTTP requestsI typing www.google.com in your browser

What about serving them?I where is the code that powers www.google.com?

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 3 / 24

Page 4: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

What Servers Do

When a client makes a request a server creates the responseI Client → Server → Client

Server:I Interprets the request (Notices it’a a GET for /somepage)I Remembers who is making the request (which IP address)I Decides what to do based on the client and the requestI Sends back a response to the client

Servers also maintain data that can change (PUT, POST,DELETE)

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 4 / 24

Page 5: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

What Are Web Frameworks?

Library that makes it easy to setup a web serveri.e. Build your own web apps!We’ll be looking at Flask todayOther examples you might’ve heard of:

I Python: Django, Tornado, Bottle, ...I Ruby: Ruby on Rails, Sinatra, ...I JavaScript & Node.js: Express.js, Meteor, ...

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 5 / 24

Page 6: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Outline

1 Web ServersWhat’s a Web Server?Flask

2 Web APIsRESTEncoding and Encryption

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 6 / 24

Page 7: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Why Flask

Micro Framework:I The minimal code needed to accept and respond to requestsI Doesn’t include many extrasI Everything you need and nothing you don’t

Extensible:I Easy to extend with extra features (libraries)I Easy to replace the few built-in extras

Actively DevelopedI Support for latest Python version and popular toolsI Bug fixes

Active CommunityI Can find answers on Stack Overflow

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 7 / 24

Page 8: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Configuring the ServerInstall: pip3 install flask

If pip3 isn’t working, click here for alternatives.Create server:

from flask import Flaskapp = Flask(__name__)

Set options:

app.debug = True

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 8 / 24

Page 9: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Handling our first RequestFirst step: Create an Endpoint

I This defines where the client is supposed to "go"

@app.route(’/’, methods=[’GET’])def my_page():

return ’Hooray Flask is working!’

Run the server: app.run()If you run the file, it will say what url to use

$ python lec11.py

* Running on http://127.0.0.1:5000/

* Restarting with reloaderHarry Smith (University of Pennsylvania) CIS 192 November 8, 2017 9 / 24

Page 10: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

What are the endpoints supposed to look like? Jinjafor Fancy HTML

Flask has Jinja2 as a built-in Template EngineAllows you to write (something like) python inside HTMLreturn render_template(’my_template.html’, arg=’my_value’)

I returns an HTML page by running a templateI Templates can take arguments

Need to put templates in a templates directory

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 10 / 24

Page 11: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Jinja Features 1: Take What’s Nice about Python andThrow it in the Garbage

Evaluate variable or expression with {{expr}}

A block names part of a template

{% block name %}some HTML{% endblock %}

for loop

{% for item in things %}<li>{{ item }}</li>

{% endfor %}

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 11 / 24

Page 12: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Jinja Features 2

if statements

{% if date %}{{ date }}

{% elif other %}{% else %}{% endif %}

Inheritance uses parent HTML but can redefined blocks

{% extends "my_template.html" %}{% block some_block_name_in_parent %}replacement content

{% endblock %}

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 12 / 24

Page 13: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Request Data and Redirects

from flask import request, redirect, url_for

@app.route(’/submit’, methods=[’GET’, ’POST’])

check which method with request.method == ’POST’

access POST data with request.form[’key’]

Access GET params with request.args.get(’key’)

return redirect(url_for(’my_page’))I Sends the user to the Flask endpoint my_page

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 13 / 24

Page 14: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Arguments in URL

A route can contain variables

@app.route(’/say/<message>’, methods=[’GET’])def url_param_example():

return render_template(’message.html’,message=message)

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 14 / 24

Page 15: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Logging

Useful for reporting errors, security inceidents, timestamps, etc.

l_name = my_flask.loglog_handler = logging.FileHandler(l_name)log_handler.setLevel(logging.DEBUG)app.logger.addHandler(log_handler)

app.logger.debug(’the message to log’)

Uses the logging standard library

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 15 / 24

Page 16: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Other Web Frameworks for Python

DjangoI A lot of work done for you: batteries included

F Admin interfaceF User modelF Easy database integration

I Great for protyping, rapid app developmentPyramid

I Configurable, flexibleI Too many options? Intimidating to start new projects

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 16 / 24

Page 17: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Outline

1 Web ServersWhat’s a Web Server?Flask

2 Web APIsRESTEncoding and Encryption

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 17 / 24

Page 18: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Principles

A REST API → Representational State TransferClient-Server: Separation of tasks (data storage vs. user state)Stateless: Each client request has all necessary infoLayered system: Client can’t tell if connected directly to serverUniform interface:

I All resources named the same way (URLs)I All messages describe how to process themselvesI State transitions are determined dynamically from the resources

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 18 / 24

Page 19: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Example REST API

Dropbox: Dropbox Core APIPrimarily uses GETs and POSTsThe endpoints allow for variables in the url

I https://api.dropbox.com/1/metadata/auto/<path>

Uses OAuth 2.0 for loginResponses are encoded with JSON

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 19 / 24

Page 20: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Outline

1 Web ServersWhat’s a Web Server?Flask

2 Web APIsRESTEncoding and Encryption

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 20 / 24

Page 21: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

Review of JSON

Many Web APIs transmit data in JSONJSON → JavaScript Object NotationData Types:

I Numbers: 25, 167.6I Strings: "firstName"I Boolean: true, falseI List: [25, "firstName", true]I Dictionary with String keys: {"fst": 1, "snd": 2}I Empty Value: null

Always wrap your JSON in a top-level dictionaryI {"data": original_JSON}I JavaScript Bug allows top-level arrays to be hacked

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 21 / 24

Page 22: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

JSON in Python

The JSON standard library: import json

json.dumps(obj) returns a JSON string of objjson.dump(obj, f_handle) writes the JSON to the filejson.loads(s) returns a Python object from a JSON stringjson.load(f_handle) returns Python object from a fileFlask has JSON: from flask import jsonify, json

I use Flask’s json.dumps()/loads()I return jsonify(d) sends a JSON response from a dictI Takes care of details like headers and encoding

requests has JSONI r = request.get(...)I r.json() parses out a Python object

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 22 / 24

Page 23: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

HTTPS SSL

If your Web App contains sensitive data → Protect ItMaking users login is a good first stepBut ... other people can listen in on HTTP requestsHTTPS uses ssl (Secure Sockets Layer)

I Fancy encryption for sending messagesI Standard way to protect data on the Web

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 23 / 24

Page 24: CIS192 Python Programmingcis192/html/files/lec/week10/lec11.pdfWhat Servers Do When a client makes a request a server creates the response I Client ! Server ! Client Server: I Interprets

What Can Go Wrong

More users are making requests than the server can handleI Solution: Have more than just a single computer as the server

Attack that specifically tries to overload server (DDoS)I Solution: Detect illegitimate requests and ignore those IPs

Bug in the server:I Infinite LoopI Arbitrary code execution

Requests and data from the internet can be harmfulI Don’t assume your server is getting good data

Harry Smith (University of Pennsylvania) CIS 192 November 8, 2017 24 / 24