Categories
Latest
Popular

Socket Programming Tutorial #4

Python Socket Programming #4

 

Hi in this lesson we are going to learn how to use libraries that have been built on top of the socket library.

 

In this tutorial we will be using httplib module which is a high-level module, and using this we will be connecting to google maps to find the coordinates of a given location.

Before we start this off , let me tell why we use these high level module rather than the socket module directly . We do this for the ease of use , and for the purpose of reusability ,because no programmer will ever want to write the same code for each of his project .  So using these modules saves time and energy of programmers .

 

Let us Start !!!

In this tutorial we will be using httplib module which is a high-level module, and using this we will be connecting to google maps to find the coordinates of a given location.

import httplib

try:

 

» import json

except ImportError: # for Python 2.5

» import simplejson as json

path = ('/maps/geo?q=207+N.+Defiance+St%2C+Archbold%2C+OH'

» » '&output=json&oe=utf8')

connection = httplib.HTTPConnection('maps.google.com')

connection.request('GET', path)

rawreply = connection.getresponse().read()

reply = json.loads(rawreply)

print reply['Placemark'][0]['Point']['coordinates'][:-

 

 

Let me explain how this code works.

  • First we import the httplib that is a library built on top of the socket module . this module  is used to interact with http servers.
  • Then we try to import the json module(JavaScript Object Notation) , this is a data interchange format . some systems don’t have this module that is why when  this command returns an error , we try to import simplejson as the json file.
  • Now we are defining the path or the http request  that will be send to the google server to be processed. The path is divided into three paths the first part is the location of the service on the server “/maps/geo?”.the second is the input string that is the location for which the coordinates are required “q=207+N.+Defiance+St%2C+Archbold%2C+OH” .the third defines type of format ,  for the reply “&output=json&oe=utf8'”.
  • Now the connection is established with the google server using the HTTPConnection function which is derived from the httplib module.
  • After connecting to the server we send a request  to google. The two parameters that are passed in this function are the GET command it tells the server that it has to send back a reply . the second parameter is the path or our query.
  • And at the end we get a reply from the server for this we use the getresponse function and calling the read function through it. The read() functions read the system buffer and retrieves the reply .  the reply is then processed by thegetresponse() function and saved to the “rawreply” variable.
  • With that we have acquired the coordinates of the location but it is still in the raw form. By the words raw form I mean that they are still in a format that is still complex so for simplifying it we pass it through the hson.loads() function with its parameters as the rawreply.
  • Then we are ready to print the coordinates on the screen.

 

*NOTE: the last statement has variables that have been passed by the server to our system and then further passed on by the json() function.

THE OUTPUT :

 

[-84.3063479, 41.5228242]

 

With this we have learned how to use a high level module. In the next module we will learn more about these high level modules.