If you want to request data from webservers, the traditional way to do that in Python is using the urllib library. While this library is effective, you could easily create more complexity than needed when building something. Is there another way?
Requests is an Apache2 Licensed HTTP library, written in Python. It’s powered by httplib and urllib3, but it does all the hard work and crazy hacks for you.
To install type:
1
2
3
|
git clone https://github.com/kennethreitz/requests.git
cd requests
sudo python setup.py install
|
The Requests library is now installed. We will list some examples below:
Grabbing raw html using HTTP/HTTPS requests
We can now query a website as :
1
2
3
|
import requests
r = requests.get('https://talkera.org/python/')
print r.content
|
Save it and run with:
1
|
python website.py
|
It will output the raw HTML code.
Download binary image using Python
1
2
3
4
5
6
7
|
from PIL import Image
from StringIO import StringIO
import requests
r = requests.get('http://1.bp.blogspot.com/_r-MQun1PKUg/SlnHnaLcw6I/AAAAAAAAA_U$
i = Image.open(StringIO(r.content))
i.show()
|
An image retrieved using python
Website status code (is the website online?)
1
2
3
|
import requests
r = requests.get('https://talkera.org/python/')
print r.status_code
|
This returns 200 (OK). A list of status codes can be found here: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Retrieve JSON from a webserver
You can easily grab a JSON object from a webserver.
1
2
3
4
5
|
import requests
import requests
r = requests.get('https://api.github.com/events')
print r.json()
|
HTTP Post requests using Python
1
2
3
4
5
6
|
from StringIO import StringIO
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text)
|
SSL verification, verify certificates using Python
1
2
3
|
from StringIO import StringIO
import requests
print requests.get('https://github.com', verify=True)
|