CSE 115
Introduction to Computer Science I
CSE 115 Introduction to Computer Science I Road map Review HTTP - - PowerPoint PPT Presentation
CSE 115 Introduction to Computer Science I Road map Review HTTP Web API's JSON in Python Examples Python Web Server import bottle @bottle.route("/") def any_name(): response =
Introduction to Computer Science I
▶︎ Review ◀ HTTP Web API's JSON in Python Examples
import bottle @bottle.route("/") def any_name(): response = "<html><body><p>" response = response + "Hello from the server!" response = response + "</p></body></html>" return response bottle.run(host="0.0.0.0", port=8080, debug=True)
import bottle
Import the library We had to install it first pip install --user bottle
bottle.run(host="0.0.0.0", port=8080, debug=True)
Run the server on port 8080
@bottle.route("/") def any_name(): response = "<html><body><p>" response = response + "Hello from the server!" response = response + "</p></body></html>" return response
Host content on the root path "/"
Web Server
Software runs continuously and waits for requests from clients Responds to requests
Client
Sends requests to server
Client
Sends requests to server
Client
Sends requests to server
How do clients send requests to a server?
Review ▶︎ HTTP ◀ Web API's JSON in Python Examples
We communicate with web servers by making HTTP requests The server will send an HTTP response in return The request is sent to a specific url in the format <protocol>://<server>/<path>?<query_string>
https://engineering.buffalo.edu/computer-science-engineering.html
import urllib.request url = "https://engineering.buffalo.edu/" url = url + "computer-science-engineering.html" response = urllib.request.urlopen(url) content = response.read().decode() print(content)
Prints the HTML for CSE@UB's homepage
import urllib.request
Import the urllib.request module Built-in to python No need to install
url = "https://engineering.buffalo.edu/" url = url + "computer-science-engineering.html" response = urllib.request.urlopen(url) content = response.read().decode() print(content)
urllib.request contains the "urlopen" function that will setup an HTTP request to a provided url and return a response object The response can be read by calling read which returns a binary string Calling decode on the binary string converts it to a string Now do anything you can do with strings. For this example we only print the response to the screen
import urllib.request url = "https://engineering.buffalo.edu/" url = url + "computer-science-engineering.html" response = urllib.request.urlopen(url) content = response.read().decode() print(content)
A set of key-value pairs key and value separated by "=" key-value pairs separated by "&" https://www.youtube.com/watch?v=5jmN_tBS0t4 Query String: "v=5jmN_tBS0t4"
https://www.google.com/search?q=cats&as_filetype=gif&lr=lang_ja
Query String: "q=cats&as_filetype=gif&lr=lang_ja"
import urllib.request url = "https://www.amazon.com" url = url + "/s?keywords=pens&sort=price-desc-rank" response = urllib.request.urlopen(url) content = response.read().decode() print(content)
Sends an HTTPS request to the server "www.amazon.com" Requests the path "/s" With a query string containing 2 key key-value pairs
Searches for the most expensive pens on amazon
I'll pass
The Internet, as most people know it, is designed for human consumption What if we want to write software that reads data from the Internet? How do we parse through the raw HTML in Python?
A web scraper is software that reads data from HTML. Many libraries exists to make this easier. We won't explore this in CSE115, though it can be a fun area to explore on your own.
Review HTTP ▶︎ Web API's ◀ JSON in Python Examples
The Internet, as most people know it, is designed for human consumption What if we want to write software that reads data from the Internet? Web APIs are hosted by web servers at urls, but instead of sending HTML/CSS/JavaScript they send raw data. Designed for programmatic consumption Typically send data as JSON
import urllib.request url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content = response.read().decode() print(content)
Connect to the open notify api Documentation: http://open-notify.org/Open-Notify-API/ISS-Location-Now/ Returns a JSON String
import urllib.request url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content = response.read().decode() print(content)
{ "message": "success", "timestamp": 1540176365, "iss_position": { "latitude": "20.6716", "longitude": "-163.2050" } }
{ "message": "success", "timestamp": 1540176365, "iss_position": { "latitude": "20.6716", "longitude": "-163.2050" } } We can get raw data, but what do we do with this JSON string? What is a JSON string?
Review HTTP Web API's ▶︎ JSON in Python ◀ Examples
JSON (JavaScript Object Notation) is a data format that can be represented as strings Send these strings to communicate across the Internet, and elsewhere All programming languages can read strings
More flexible than CSV
Only 6 different data types
as a number. Should be formatted as an integer or floating point number
[{"title":"God Am (Live 1996)","artist":"Alice in Chains","ratings": [5,4],"youtubeID":"74P4W_okEqA"},{"title":"Fade to Black","artist":"Metallica","ratings":[5,2],"youtubeID":"WEQnzs8wl6E"}]
Closely resembles Javascript and Python syntax that we've seen, except it is a
publications/files/ECMA-ST/ECMA-404.pdf for more info.
import urllib.request import json url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content)
Use the built-in json module to handle JSON strings Call json.loads to convert a JSON string to python types
import urllib.request import json url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content)
{ 'message': 'success', 'iss_position': { 'longitude': '-110.1453', 'latitude': '-39.6226'}, 'timestamp': 1540177620 }
import urllib.request import json url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content['iss_position']['longitude']) print(content['iss_position']['latitude'])
The result looks similar to the JSON string, but now it is a Python dictionary instead of a string We can use dictionary functions to process the data
import urllib.request import json url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content['iss_position']['longitude']) print(content['iss_position']['latitude'])
*Note: The numbers are different across slides since each time the code is executed we are getting the current location of the ISS. With web APIs we can work with live data!
Review HTTP Web API's JSON in Python ▶︎ Examples ◀
Big list of APIs
Hackathon next week
covered so far!