REST vs SOAP Web Services

I am seeing a lot of new web services are implemented using a REST style architecture these days rather than a SOAP one. Lets step back a second and explain what REST is.

What is a REST Web Service

The acronym REST stands for Representational State Transfer, this basically means that each unique URL is a representation of some object. You can get the contents of that object using an HTTP GET, to delete it, you then might use a POST, PUT, or DELETE to modify the object (in practice most of the services use a POST for this).

Who’s using REST?

All of Yahoo’s web services use REST, including Flickr, del.icio.us API uses it, pubsub, bloglines, technorati, and both eBay, and Amazon have web services for both REST and SOAP.

Who’s using SOAP?

Google seams to be consistent in implementing their web services to use SOAP, with the exception of Blogger, which uses XML-RPC. You will find SOAP web services in lots of enterprise software as well.

REST vs SOAP

As you may have noticed the companies I mentioned that are using REST api’s haven’t been around for very long, and their apis came out this year mostly. So REST is definitely the trendy way to create a web service, if creating web services could ever be trendy (lets face it you use soap to wash, and you rest when your tired). The main advantages of REST web services are:

Lightweight – not a lot of extra xml markup Human Readable Results Easy to build – no toolkits required SOAP also has some advantages:

Easy to consume – sometimes Rigid – type checking, adheres to a contract Development tools For consuming web services, its sometimes a toss up between which is easier. For instance Google’s AdWords web service is really hard to consume (in CF anyways), it uses SOAP headers, and a number of other things that make it kind of difficult. On the converse, Amazon’s REST web service can sometimes be tricky to parse because it can be highly nested, and the result schema can vary quite a bit based on what you search for.

Which ever architecture you choose make sure its easy for developers to access it, and well documented.

Advertisements

The best module for ssh (secure shell) connection to remote mechine.

U want to connect remote machine using ssh through the programmatically.

About Paramiko

Paramiko is a pure-Python module and can be easy_install’ed as other typical python modules can. However, PyCrypto is written largely in C, so you may need a compiler to install both depending on your platform.

Paramiko itself has extensive API documentation and an active mailing list. As an added bonus, there’s a Java port of it as well (don’t get me started on controlling SSH within Java) if you need something to achieve the same thing in Java.

Paramiko also offers an implementation of the SSH and SFTP server protocols. It really is feature-rich and complete. I’ve used it in heavily threaded applications as well as in day-to-day maintenance scripts. There’s even an installation and deployment system, named Fabric, that further builds on Paramiko to provide application deployment utilities via SSH.

Getting started

The primary class of the Paramiko API is ”paramiko.SSHClient”. It provides the basic interface you are going to want to use to instantiate server connections and file transfers.

Here’s a simple example:

import paramiko
ssh = paramiko.SSHClient()
ssh.connect('127.0.0.1', username='jesse', 
    password='lol')

This creates a new SSHClient object, and then calls ”connect()” to connect us to the local SSH server. It can’t get much easier than that!

Host Keys

One of the complicating aspects of SSH authentication is host keys. Whenever you make an ssh connection to a remote machine, that host’s key is stored automatically in a file in your home directory called ”.ssh/known_hosts”. If you’ve ever connected to a new host via SSH and seen a message like this:

The authenticity of host 'localhost (::1)' can't be
established.
RSA key fingerprint is 
22:fb:16:3c:24:7f:60:99:4f:f4:57:d6:d1:09:9e:28.
Are you sure you want to continue connecting 
(yes/no)? 

and typed “yes” — you’ve added an entry to the ”known_hosts” file. These keys are important because accepting them implies a level of trust of the host. If the key ever changes or is compromised in some way, your client will refuse to connect without notifying you.

Paramiko enforces this same rule. You must accept and authorize the use and storage of these keys on a per-host basis. Luckily, rather then having to be prompted for each one, or manage each one individually, you can set a magic policy.

The default behavior with an SSHClient object is to refuse to connect to a host (”paramiko.RejectPolicy”) who does not have a key stored in your local ”known_hosts” file. This can become annoying when working in a lab environment where machines come and go and have the operating system reinstalled constantly.

Setting the host key policy takes one method call to the ssh client object (”set_missing_host_key_policy()”), which sets the way you want to manage inbound host keys. If you’re lazy like me, you pass in the ”paramiko.AutoAddPolicy()” which will auto-accept unknown keys.

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse', 
    password='lol')

Of course, don’t do this if you’re working with machines you don’t know or trust! Tools built on Paramiko should make this overly liberal policy a configuration option.

Running Simple Commands

So, now that we’re connected, we should try running a command and getting some output.

SSH uses the same type of input, output, and error handles you should be familiar with from other Unix-like applications. Errors are sent to standard error, output goes to standard out, and if you want to send data back to the application, you write it to standard in.

So, the response data from client commands are going to come back in a tuple – (stdin, stdout, stderr) – which are file-like objects you can read from (or write to, in the case of stdin). For example:

...
>>> ssh.connect('127.0.0.1', username='jesse', 
...    password='lol')
>>> stdin, stdout, stderr = \
...    ssh.exec_command("uptime")
>>> type(stdin)

>>> stdout.readlines()
['13:35  up 11 days,  3:13, 4 users, load averages: 0.14 0.18 0.16\n']

Under the covers, Paramiko has opened a new ”paramiko.Channel” object which represents the secure tunnel to the remote host. The Channel object acts like a normal python socket object. When we call ”exec_command()”, the Channel to the host is opened, and we are handed back ”paramiko.ChannelFile” “file-like” objects which represents the data sent to and from the remote host.

One of the documented nits with the ChannelFile objects paramiko passes back to you is that you need to constantly ”read()” off of the stderr and stdout handles given back to you. If the remote host sends back enough data to fill the buffer, the host will hang waiting for your program to read more. A way around this is to either call ”readlines()” as we did above, or ”read()”. If you need to internally buffer the data, you can also iterate over the object with ”readline()”.

This is the simplest form of connecting and running a command to get the output back. For many sysadmin tasks, this will be invaluable as you need to parse the output of a returned command to find exactly what you need. With Python’s rich string manipulation, this is an easy task. Let’s run something with a lot of output, that also requires a password:

ssh.connect('127.0.0.1', username='jesse', 
   password='lol')
stdin, stdout, stderr = ssh.exec_command(
   "sudo dmesg")

Uh oh. I just called the sudo command. It is going to require me to provide a password interactively with the remote host. No worries:

ssh.connect('127.0.0.1', username='jesse', 
    password='lol')
stdin, stdout, stderr = ssh.exec_command(
    "sudo dmesg")
stdin.write('lol\n')
stdin.flush()
data = stdout.read.splitlines()
for line in data:
    if line.split(':')[0] == 'AirPort':
        print line

There! I logged in remotely and found all messages for my Airport card. The key thing to note here is that I wrote my password to the stdin “file” so that sudo allowed me in.

If you’re wondering, yes, this provides an easy base to create your own interactive shell. You might want to do something like this to make a little custom admin shell using the Python cmd module to administer machines inside of your lab.

File put and get

File manipulation within Paramiko is handled via the SFTP implementation, and, like the ssh client command execution, it’s easy as pie.

We start by instantiating a new paramiko.SSHClient just as before:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse', 
    password='lol')

This time, we make a call into ”open_sftp()” after we perform the connect to the host. ”open_sftp()” returns a ”paramiko.SFTPClient” client object that supports all of the normal sftp operations (stat, put, get, etc.). In this example, we perform a “get” operation to download the file ”remotefile.py” from the remote system and write it to to the local file, ”localfile.py”.

ftp = ssh.open_sftp() ftp.get('remotefile.py', 'localfile.py') ftp.close()

Writing a file to the remote host (a “put” operation) works the exact same way. We just transpose the local and remote arguments:

ftp = ssh.open_sftp()
ftp.get('localfile.py', 'remotefile.py')
ftp.close()

The nice thing about the sftp client implementation that Paramiko provides is that it support things like stat, chmod, chown, etc. Obviously these might act differently depending on the remote server because some servers do not implement all of the protocol, but even so they’re incredibly useful.

You could easily write functions like ”glob.glob()” to transverse a remote directory tree looking for a particular filename pattern. You could also search based on permissions, size, etc.

One thing to note, however, and this bit me a few times: sftp as a protocol is slightly more restrictive than something like normal secure copy (scp). SCP allows you to use Unix wild cards in the file name when grabbing a file from the remote machine. SFTP, on the other hand, expects the full explicit path to the file you want to download. An example of this is:

ftp.get('*.py', '.')

In most cases, this would mean “download all files with .py” to the local directory on my machine. SFTP is unhappy with this formulation, though (see Listing 2). I learned this the hard way, after I spent several hours pulling apart the sftp client implementation out of frustration.

 

 

The best way to learn Python.

Oh!! Finally decided to learn Python.Ha ,Good, Then Fallow the below steps.

Requirements to learn Python:

1  U should be a human-being

2 U must have lap or desktop

3 U must install Python in your device

Step-1:  Install Python in your device

For Windows:

Go to python official website (https://www.python.org/) and download exe file , install in your system (better to install 2.7.3 version)

For Linux:

Go to Python official website (https://www.python.org/) and download tar file , unzip it run python setup file, before installing python in Linux system check once in your system u might have already , most of the Linux system’s have python by default.

Step-2 Open your Python IDLE from start bar

Now in your hand u have book , then start writing on it, i.e practice some basic stuff on it like 2+2….

Then take any basic Python PDF ex: Head First Programming Python (https://powerofpython.wordpress.com/tutorials/)  or open Tutorial Point website (http://www.tutorialspoint.com/python/) .

First start with basic introduction of python programming language then step by step.

And practice each concept by example .

If u are comfort with basic python stuff, start learning some advance concepts with help of  books like Dive Into Python (http://www.diveintopython.net).

Step-3 Play with Python programs.

Now u know some python , then it’s time to write some useful code , for this do goggling find some basic application level code , and practice it.

Step-4 Take step for useful.

Now you are Jim in Python , don’t feel that you are ready for industry , there is one more step , i.e need to learn combination of technologies.

Now  decide which side need to go , to become hero in industry , i.e in Web development or GUI development or Automation or some other.

Step-5 Learn Django for web development.

Now in your hand have Python, open Django official website (https://www.djangoproject.com/) find some example app , and practice it.

Finally!!!!!! Ha….Become…Pythonist…Congrats ..All The Best For Ur Future….

Want to Start New Django Project Fallow These Steps (Django Workflow).

Django Workflow

  1. Create a new project and a new app called mainapp
  2. Edit settings.py. Fix the following:
    1. Database settings
    2. Installed apps
    3. Media and media admin paths
    4. Most recently, to add CSS and JS paths
  3. Edit urls.py in the project folder and
    1. Uncomment the admin paths
    2. Include mainapp.urls.py
  4. Create a new urls.py in the mainapp folder
    1. Create a path that display a view called home
  5. Create a new templates folder
  6. Create a base.html file in the templates folder
    1. Create a place-holder variable within base.html
  7. Edit the views.py file. Create a new view called home and return ‘hello world’ using the base.html template and a place-holder variable
  8. Create a media folder the project folder (1 level up from the mainapp folder)
  9. Edit the models.py file
  10. Run syncdb
  11. At this point, you should be able to access both your admin page and your homepage that outputs ‘hello world’ by running the development server

__name__ == “__main__” in Python

When you run a python script directly (example: $ python script.py), you want to set a starting point of the script. Python scripts are interpreted from the first line, then goes to the second line and so on …

import module

def my_function():

code here

x = my_function()

But you want to make your code more structured, so you come up with this:

import module

def my_function():

code here

def main():
x = my_function()

the program starts from here

main()

This is good, but problem is, if you import the script from another script (from module import *), the main() function gets executed, but you may not want to do this. You want to call the main() function only when this script is exclusively executed. And you can do this using name == “main“.

import module

def my_function():

code here

def main():
x = my_function()

the program starts from here

if name == “main“:
main()

Thus you can make your script to make it a reusable script (import from another script) and a standalone script as well.

Best Python Learning Web Links

http://learnpythonthehardway.org

http://www.tutorialspoint.com/python/

http://effbot.org

http://www.programiz.com/python-programming/

http://www.learnpython.org/

http://www.diveintopython.net

http://en.wikibooks.org/wiki/Python_Programming

http://planet.python.org/

http://love-python.blogspot.in

http://python-history.blogspot.in/

http://lucumr.pocoo.org/

http://www.blog.pythonlibrary.org/

https://developers.google.com/edu/python

http://www.pythonforbeginners.com

http://www.python-course.eu

http://www.secnetix.de/olli/Python/

http://code.activestate.com/recipes/langs/

http://www.sthurlow.com/python/

http://www.pythoncentral.io/