python_wiki:requests

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python_wiki:requests [2018/08/09 23:19]
billdozor [Requests]
python_wiki:requests [2019/05/25 23:50] (current)
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: Sites to Practice Using Requests Against:
   * Generic http request and response server: http://httpbin.org/   * Generic http request and response server: http://httpbin.org/
Line 36: Line 37:
 Requests supports the following http request types. Requests supports the following http request types.
 <code python> <code python>
-my_site = "http://www.mysite.org/"+my_site = "http://httpbin.org/"
  
 # GET # GET
-response = request.get(my_site)+response = requests.get(my_site + "get")
  
 # POST # POST
-response = request.post(my_site)+response = requests.post(my_site + "post")
  
 # PUT # PUT
-response = request.put(my_site)+response = requests.put(my_site + "put")
  
 # DELETE # DELETE
-response = request.delete(my_site)+response = requests.delete(my_site + "delete")
  
 +##-- 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)
 </code> </code>
  
Line 88: Line 90:
 print("Comic Text: " + data['alt']) print("Comic Text: " + data['alt'])
 print("Comic Date: " + data['year'] + "-" + data['month'] + "-" + data['day']) print("Comic Date: " + data['year'] + "-" + data['month'] + "-" + data['day'])
 +print("Comic Image URL: " + data['img'])
 </code> </code>
  
Line 97: Line 100:
  
 -- The Current XKCD Comic -- -- The Current XKCD Comic --
-Comic Title: Complex Numbers +Comic Title: Pie Charts 
-Comic Text: I'm trying to prove that mathematics forms a meta-abelian groupwhich would finally confirm my suspicions that algebreic geometry and geometric algebra are the same thing+Comic Text: If you can't get your graphing tool to do the shadingjust add some clip art of cosmologists discussing the unusual curvature of space in the area
-Comic Date: 2018-8-3+Comic Date: 2018-8-10 
 +Comic Image URL: https://imgs.xkcd.com/comics/pie_charts.png
 </code> </code>
  
Line 107: 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>
 +#!/usr/bin/python
 +
 +import requests
 +
 +# Post the request (this will delay/sleep for 5 seconds)
 +response = requests.post("http://httpbin.org/delay/5")
 +
 +# Extract the JSON object from the response
 +data = response.json()
 + 
 +# Show http status code
 +print("Status code is:" + str(response.status_code))
 +
 +print("Returned response is:")
 +print(data)
 +</code>
  
 ---- ----
  
  • python_wiki/requests.1533871160.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)