Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python_wiki:requests [2018/08/05 16:14] billdozor [GET] |
python_wiki:requests [2018/08/11 15:24] billdozor [GET] |
||
---|---|---|---|
Line 5: | Line 5: | ||
The requests library is a http library that allows you to interact with web servers via Python. | The requests library is a http library that allows you to interact with web servers via Python. | ||
+ | \\ | ||
+ | Sites to Practice Using Requests Against: | ||
+ | * Generic http request and response server: http:// | ||
+ | * XKCD Comics: https:// | ||
+ | |||
+ | \\ | ||
**Checklist** | **Checklist** | ||
* Python 2 or 3 | * Python 2 or 3 | ||
Line 31: | Line 37: | ||
Requests supports the following http request types. | Requests supports the following http request types. | ||
<code python> | <code python> | ||
- | my_site = " | + | my_site = " |
# GET | # GET | ||
- | response = request.get(my_site) | + | response = requests.get(my_site |
# POST | # POST | ||
- | response = request.post(my_site) | + | response = requests.post(my_site |
# PUT | # PUT | ||
- | response = request.put(my_site) | + | response = requests.put(my_site |
# DELETE | # DELETE | ||
- | response = request.delete(my_site) | + | response = requests.delete(my_site |
+ | ##-- head and options responses are not implemented in the httpbin.org website --## | ||
# HEAD | # HEAD | ||
- | response = request.head(my_site) | + | response = requests.head(my_site) |
# OPTIONS | # OPTIONS | ||
- | response = request.options(my_site) | + | response = requests.options(my_site) |
</ | </ | ||
Line 59: | Line 66: | ||
\\ | \\ | ||
- | Example: Retrieve the latest XKCD Comic Info | + | Example: Retrieve the latest XKCD Comic Info ( More Info: https:// |
<code python> | <code python> | ||
# | # | ||
Line 83: | Line 90: | ||
print(" | print(" | ||
print(" | print(" | ||
+ | print(" | ||
</ | </ | ||
Line 92: | Line 100: | ||
-- The Current XKCD Comic -- | -- The Current XKCD Comic -- | ||
- | Comic Title: | + | Comic Title: |
- | Comic Text: I'm trying | + | Comic Text: If you can't get your graphing tool to do the shading, just add some clip art of cosmologists discussing |
- | Comic Date: 2018-8-3 | + | Comic Date: 2018-8-10 |
+ | Comic Image URL: https:// | ||
</ | </ | ||
Line 102: | Line 111: | ||
Send data in a POST request. | Send data in a POST request. | ||
+ | |||
+ | \\ | ||
+ | Example: Use the httpbin.org site to delay 5 seconds (will replace with a more interesting example later) | ||
+ | <code bash> | ||
+ | # | ||
+ | |||
+ | import requests | ||
+ | |||
+ | # Post the request (this will delay/sleep for 5 seconds) | ||
+ | response = requests.post(" | ||
+ | |||
+ | # Extract the JSON object from the response | ||
+ | data = response.json() | ||
+ | |||
+ | # Show http status code | ||
+ | print(" | ||
+ | |||
+ | print(" | ||
+ | print(data) | ||
+ | </ | ||
---- | ---- | ||