Requests is an elegant and simple Apache2 licensed HTTP library for Python. It is designed to be used by humans to interact with the language. This means you don’t have to manually add query strings to URLs, or form-encode your POST data.
Requests will allow you to send HTTP/1.1 requests using Python. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries. It also allows you to access the response data of Python in the same way.
Here, I am installing using pip. For more options, visit.
1 |
pip install requests |
First lets import requests
1 |
import requests |
Here, i am using a webpage within my server which open to a page like this.
Let’s create a Response Object r
1 |
r = requests.get('http://localhost/hello.php') |
Let’s check the current status of the site
1 2 3 |
r.status_code #200 |
1 2 3 |
r.status_code == requests.codes.ok #True |
1 2 3 |
requests.codes['temporary_redirect'] #307 |
1 2 3 |
requests.codes.teapot #418 |
Let’s get the content of the site
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
print r.text """ <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <h2>PHP Form Validation Example</h2> <p><span class="error">* required field.</span></p> <form method="post" action="/hello.php"> Name: <input type="text" name="name" value=""> <span class="error">* </span> <br><br> E-mail: <input type="text" name="email" value=""> <span class="error">* </span> <br><br> Website: <input type="text" name="website" value=""> <span class="error"></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <span class="error">* </span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <h2>Your Input:</h2><br><br><br><br> </body> </html> """ |
To know what encoding it uses,
1 2 3 |
r.encoding #'UTF-8' |
Let’s change that to say,
1 |
r.encoding = 'ISO-8859-1' |
You might want to do this in any situation where you can apply special logic to work out what the encoding of the content will be. For example, HTTP and XML have the ability to specify their encoding in their body. In situations like this, you should use r.content to find the encoding, and then set r.encoding. This will let you use r.text with the correct encoding.
Requests will also use custom encoding in the event. If you have created your own encoding and registered it with the codecs module, you can simply use the codec name as the value of r.encoding and Requests will handle the decoding for you.
There’s also a builtin JSON decoder, in case you’re dealing with JSON data:
1 |
r.json() |
With every request you send to a HTTP server, the server will send you some additional data. You can get extract data from an HTTP response using:
1 2 3 |
print r.headers """{'Content-Length': '393', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Keep-Alive': 'timeout=5, max=100', 'Server': 'Apache/2.4.10 (Debian)', 'Connection': 'Keep-Alive', 'Date': 'Fri, 07 Jul 2017 14:09:24 GMT', 'Content-Type': 'text/html; charset=UTF-8'}""" |
This is the list of features from the requests site:
An awesome lotta features!Right?
To know more,
http://docs.python-requests.org/en/master/
http://docs.python-requests.org/en/latest/api/
http://pypi.python.org/pypi/requests
http://docs.python-requests.org/en/latest/user/quickstart/
http://isbullsh.it/2012/06/Rest-api-in-python/#requests
http://docs.python-requests.org/en/master/user/quickstart/
Leave a Reply