Raspberry Pi making HTTP requests using Python
HTTP GET and POST requests can be used to communicate with web servers ,api etc. Using this method you can develop some cool apps with your Raspberry Pi. In this tutorial we are going to see how to make HTTP GET and POST requests with your Raspberry Pi.
Making HTTP GET requests
In Raspberry Pi the HTTP GET requests can be made using the package urllib. The code is very simple. By using the method urllib.urlopen() you can get the contents of the page. In this example I am making HTTP GET request to the url “http://api.learn2crack.com/rpi/rpi_get.php”. It will print the output as “Hello Raspberry Pi”.
Example
Making HTTP POST requests
Http POST requests can be made using the package urllib and urllib2. The post parameters should be in the format of { ‘data’:’data’ }. The post request can be made using the method urllib2.Request(). By using the method urllib2.urlopen() you can get the contents of the page. In this example I am making HTTP POST request to the url “http://api.learn2crack.com/rpi/rpi_post.php”. It will print the output as “State is : Active”.
Example
Steps to execute the code
1. Open the Raspberry Pi terminal.
2. Then type nano program.py
3. Now type the python program and save it.
4. Execute using the command python program.py
Comments
Post a Comment