How develop and deploy an application in the cloud. A referencial architecture (Part 1)

February 10th, 2014

Link: https://github.com/rancavil/django-py3-openshift-quickstart/wiki/How-develop-and-deploy-an-application-in-the-cloud.-A-referencial-architecture-(Part-1).

See how we can implement an Architecture using the cloud services, Openshift Django / Python and Redis in the Cloud.

Python and MongoLab (Mongo in the Cloud)

January 19th, 2014

This is a little tutorial how use MongoDB on the Cloud.

First we need find Mongo in the cloud...I will use MongoLab for demonstrate how work pymongo.

We need to create a account on https://mongolab.com and complete the form https://mongolab.com/signup/

Follow the instructions to create a database and create a user on MongoLab web console.

To connect using a driver via the standard URI:

mongodb://<dbuser>:<dbpassword>@ds0<port>.mongolab.com:<port>/<database>

When you create a database on MongoLab a port is assigned to the resource.

For our example, we create a database called db_posts.

Assume that port 28012 is created

mongodb://<dbuser>:<dbpassword>@ds028012.mongolab.com:28012/db_posts

Important: you must create the dbuser (https://mongolab.com/databases/db_posts#users), this is not the user account.

Well, now we go to write the python code.

Create the create.py file to insert a document into the collection:

1  from pymongo import MongoClient
2
3  client = MongoClient('ds028012.mongolab.com:28012')
4  client.db.authenticate('joe','pass12345',source='db_posts')
5 
6  db = client.db_posts
7  posts = db.posts
8  post = {'author':'rodrigo','text':'Hello world'}
9
10 id_post = posts.insert(post)
11 print 'Create the id : %s'%id_post
12 
13 client.close()

Create the read.py file to retrieve documents from the collection:

1  from pymongo import MongoClient
2
3  client = MongoClient('ds028012.mongolab.com:28012')
4  client.db.authenticate('joe','pass12345',source='db_posts')
5 
6  db = client.db_posts
7
8  results_posts = db.posts.find()
9  for post in results_posts:
10     print post
11 
12 client.close()

I hope with this you can see how work python and mongo in the cloud, If you like MongoHQ, that is similar to use MongoLab.

Enjoy!!!

The Classic Hello World!!!

January 4th, 2012

We will develop the classic hello world!!! like a SOAP web service.

The purpose of this example is to explain how tornado-webservices works, with the use of the classic Hello World.

Note: For this example you must to use tornadows-0.9.3.2.

Step 1: Create a file named HelloService.py

1  import tornado.httpserver
2 import tornado.ioloop
3 from tornadows import soaphandler
4 from tornadows import webservices
5 from tornadows.soaphandler import webservice
6
7 class HelloWorldService(soaphandler.SoapHandler):
8 @webservice(_params=None,_returns=str)
9 def sayHello(self):
10 return 'Hello World!!!'
11
12 if __name__ == '__main__':
13 service = [('HelloService',HelloWorldService)]
14 app = webservices.WebService(service)
15 ws = tornado.httpserver.HTTPServer(app)
16 ws.listen(8080)
17 tornado.ioloop.IOLoop.instance().start()

First, the class HelloWorldService (lines 7 to 10) is the service that holds the operation or methods that contains the logical of the service.

With the decorator @webservice, indicates that the method sayHello () (lines 8 to 10) is the operation of the service.

In the line 13, service = [('HelloService','HelloWorldService')], indicates the name of the service that will be published on the net.

The tuple ('HelloWorld','HelloWorldService') associates the name of the service with the class of python. By this way the service can be accesed like:

http://localhost:8080/HelloService?wsdl

Finally, in  the line 16, indicates that we are listening from the port 8080.

Step 2: Running the service.

Simply execute:

$ python HelloService.py

Step 3: Testing with a client.

Now, I will created a client to HelloService with suds (0.3.7).

Create a file named ClientHelloService.py with this code:

1  import suds
2
3 url = 'http://localhost:8080/HelloService?wsdl'
4 client = suds.client.Client(url,cache=None)
5 print client.service.sayHello()

Note that we don't pass any parameters to the operation sayHello().

line 1: we import the api python-suds.

line3: indicates the url with the Web Service Description Language (WSDL), in other words, the location of web service.

line 4: is created the client of the web service.

line 5: finally, is printed the result. The result should be (of course): "Hello World!!!"

For running the client, simply execute:

$ python ClientHelloWorld.py

Hello World!!!

You can see more examples in:

https://github.com/rancavil/tornado-webservices/tree/master/demos

And documentation in:

https://github.com/rancavil/tornado-webservices/wiki

You can download directly from:

https://github.com/rancavil/tornado-webservices/downloads/tornadows-0.9.3.2.tar.gz

For check prerequisites and install see:

https://github.com/rancavil/tornado-webservices/wiki/Python-Web-Services-with-Tornado

 

Python classes to XML/XSD

September 11th, 2011

If you want to test a python module that transforms python classes to XML and XSD documents look at:

https://github.com/rancavil/tornado-webservices/downloads/complextypes.tar.gz


Web Services and ComplexTypes

August 13th, 2011

It incorporates the mechanism for the use of python classes like xml complex types.

You must downloads and install the new release:

https://github.com/rancavil/tornado-webservices/downloads/tornadows-0.9.1.tar.gz

(Don't forget, you need have installed tornado web server).

In the example, the web service allows the consultation of products stored in a database.

Create the file ProductService.py

import tornado.httpserver
import tornado.ioloop
from tornadows import soaphandler
from tornadows import webservices
from tornadows import complextypes
from tornadows.soaphandler import webservice

class Input(complextypes.ComplexType):
idProduct = complextypes.IntegerProperty()

class Product(complextypes.ComplexType):
id = complextypes.IntegerProperty()
name = complextypes.StringProperty()
price = complextypes.FloatProperty()
stock = complextypes.IntegerProperty()

class ProductService(soaphandler.SoapHandler):
@webservice(_params=Input,_returns=Product)
def getProduct(self, input):
id = input.idProduct.value

reg = self.database(id)

output = Product()
output.id.value = id
output.name.value = reg[0]
output.price.value = reg[1]
output.stock.value = reg[2]

return output

def database(self,id):
""" This method simulates a database of products """
db = {1:('COMPUTER',1000.5,100),
2:('MOUSE',10.0,300),
3:('PENCIL BLUE',0.50,500),
4:('PENCIL RED',0.50,600),
5:('PENCIL WHITE',0.50,900),
6:('HEADPHONES',15.7,500),
}
row = (None,0.0,0)
try:
row = db[id]
except:
None
return row

if __name__ == '__main__':
service = [('ProductService',ProductService)]
app = webservices.WebService(service)
ws = tornado.httpserver.HTTPServer(app)
ws.listen(8080)
tornado.ioloop.IOLoop.instance().start()

Executing the service with.

$ python ProductService.py

You can see the WSDL from http://localhost:8080/ProductService?wsdl

For testing the service, you can use SoapUI or use python-suds.

You can use the next code:

import suds

url = 'http://localhost:8080/ProductService?wsdl'
client = suds.client.Client(url)

print client
# Param IdProduct, you can test with 1,2,3...
idProduct = 1
output = client.factory.create('Product')
output = client.service.getProduct(idProduct)

print 'Result'
print '---------------------------------'
print 'Id.Product : %d'%output.id
print 'Name Product : %s'%output.name
print 'Price : %f'%output.price
print 'Stock : %d'%output.stock
print '---------------------------------'
print

You need have installed python-suds, you can download it from:

https://fedorahosted.org/releases/s/u/suds/python-suds-0.3.7.tar.gz

Now, you can running the script for test the service.