<jupyter_start><jupyter_text>IST256 Lesson 10 HTTP and Network Programming- Assigned Readings From    https://ist256.github.io/spring2021/readings/Web-APIs-In-Python.html Links- Participation: [https://poll.ist256.com](https://poll.ist256.com)  (Sign in with *<EMAIL> and your SU Password.)- Class Chat: [https://chat.ist256.com](https://chat.ist256.com)  (Microsoft Teams.)  Agenda- Homework- How the Web Works- Making HTTP requests using the Python requests module- Parsing json responses into Python objects- Procedure for calling API's- How to read API documentation  Project (49 out of 250 points)- http://ist256.com/syllabus/project-p1-p4- No grade until the end. only feedback.- Project documents will be released after the 3rd exam.  FEQT (Future Exam Questions Training) 1What is the output of the following code?<jupyter_code>x = { 'a' : [1,2,3,4], 'b' : 'rta', 'c': { 'r' : 3, 't' : 2} }
print( type(x['a']) )<jupyter_output><class 'list'><jupyter_text>A. `str`  B. `int`  C. `dict`  D. `list`    Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  FEQT (Future Exam Questions Training) 2What is the output of the following code?<jupyter_code>x = { 'a' : [1,2,3,4], 'b' : 'rta', 'c': { 'r' : 3, 't' : 2} }
print( type(x['b'][1]) )<jupyter_output><class 'str'><jupyter_text>A. `str`  B. `int`  C. `dict`  D. `list`    Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  FEQT (Future Exam Questions Training) 3What is the output of the following code?<jupyter_code>x = { 'a' : [1,2,3,4], 'b' : 'rta', 'c': { 'r' : 3, 't' : 2} }
print( x['a'][2])<jupyter_output>3<jupyter_text>A. `2`  B. `3`  C. `KeyError`  D. `IndexError`    Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  FEQT (Future Exam Questions Training) 4What is the output of the following code?<jupyter_code>x = { 'a' : [1,2,3,4], 'b' : 'rta', 'c': { 'r' : 3, 't' : 2} }
print( x['b'][4] )<jupyter_output><empty_output><jupyter_text>A. `2`  B. `3`  C. `KeyError`  D. `IndexError`    Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  FEQT (Future Exam Questions Training) 5What is the output of the following code?<jupyter_code>x = { 'a' : [1,7,3,4], 'b' : 'rta', 'c': { 'r' : 3, 't' : 2} }
print( x['c'] )<jupyter_output>{'r': 3, 't': 2}<jupyter_text>A. `2`  B. `3`  C. `KeyError`  D. `IndexError`    Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  FEQT (Future Exam Questions Training) 6What is the output of the following code?<jupyter_code>x = { 'a' : [1,2,3,4], 'b' : 'rta', 'c': { 'r' : 3, 't' : 2} }
print( x['c']['r'])<jupyter_output><empty_output><jupyter_text>A. `2`  B. `3`  C. `KeyError`  D. `IndexError`    Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  Connect Activity**Question:** The Python module to consume Web API's is called:  A. `api`  B. `requests`  C. `http`  D. `urllibrary`   Vote Now: [https://poll.ist256.com](https://poll.ist256.com)   What is the Big Picture Here?  - First learned how to call functions built into python like, `input()` and `int()` - Then we larned how to `import` a module of functions then use them, like `math` or `json`, or `ipywidgets` - Then learned how to find new code on http://pypi.org, install with `pip` and then `import` to use it, like `gtts` or `emoji` - Then we learned built-in functions of variables of type `str` `list` and `dict` such as `str.find()` and `dict.keys()`   - **Now we will learn how to call functions over the internet, executing code remotely!!!**       HTTP: The Protocol of The Web- When you type a URL into your browser you’re making a **request**.- The site processing your request sends a **response**.- Part of the response is the **status code**. This indicates “what happened”- The other part of the response is **content** (this is usually HTML) which is rendered by the browser.- HTTP is  a **text based** protocol. It is **stateless** meaning each request in independent of the other.  HTTP Request VerbsHTTP Request Verbs:- GET - used to get resources- POST - used to send large data payloads as input- PUT - used for updates- DELETE - used to delete a resource  HTTP Response Status codesThe HTTP response has a payload of data and a status code. HTTP Status Codes:- `1xx` Informational- `2xx` Success- `3xx` Redirection- `4xx` Client Error- `5xx` Server Error  Watch Me Code 1 A Non-Python Demo of HTTP- What happens when you request a site? Like http://www.syr.edu ?- Chrome Developer tools- Now using `requests`.- Status codes and request verbs.- de-serializing `json` output.  Check Yourself: Response CodesThe HTTP Response code for **success** is  A. `404`  B. `501`  C. `200`  D. `301`   Vote Now: [https://poll.ist256.com](https://poll.ist256.com)  4 Ways to Send Data over HTTP1. In the URL  `GET http://www.someapi.com/user/45`2. On the Query String - a set of key-value pairs on the URL   `GET http://www.someapi.com?user=45`  3. In the request header  - a set of key-value pairs in the HTTP header  `header = { 'user' : 45 } GET http://www.someapi.com`4. In the body of an HTTP post - any format   `Body: user=45 POST http://www.someapi.com`Which approach do you use? Depends on the service you are using!  Watch Me Code 2Examples of the many ways send data over HTTP using the https://httpbin.org/  website (Wait, scratch that, using https://api.ist256.com) !!!- HTTP GET in the url- HTTP GET in the query string and url generation- HTTP GET in the header- HTTP POST- Combinations  Check Yourself : HTTP MethodsWhat is the URL printed on the last line?<jupyter_code>import requests
params = { 'a' : 1, 'b' : 2 }
headers = { 'c' : '3'}
url = "https://httpbin.org/get"
response = requests.get(url, params = params, headers = headers)
print(response.url)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15419501133786848, 'fraction_numerical': 0.03087388801674516, 'mean_word_length': 4.007860262008734, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 24, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '123480', 'license': 'Apache-2.0', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1968, 'n_words': 733, 'path': 'lessons/10-http/Slides.ipynb', 'repo_name': 'IST256/spring2022', 'size': '5740'}