Luca F.
Luca F.

Reputation: 113

REST web service with Python using WSME

I'm trying to create a simple REST Web Service using technology WSME reported here:

https://pypi.python.org/pypi/WSME

It's not clear, however, how to proceed. I have successfully installed the package WSME.0.6.4 but I don't understand how to proceed. On the above link we can see some python code. If I wanted to test the code what should I do? I have to create a .py file? Where this file should be saved? Are there services to be started? The documentation is not clear: it says "With this published at the / ws path of your application". What application? Do I need to install a Web Server?

Thanks.

Upvotes: 0

Views: 688

Answers (1)

Alexander
Alexander

Reputation: 12795

You could use a full blown web server to run your application . For example Apache with mod_wsgi or uWSGI , but it is not always necessary .

Also you should choose a web framework to work with .
According WSME doc's it has support for Flask microframework out of the box , which is simple enough to start with .

To get started create a file with the following source code :

from wsgiref.simple_server import make_server
from wsme import WSRoot, expose

class MyService(WSRoot):
    @expose(unicode, unicode)
    def hello(self, who=u'World'):
        return u"Hello {0} !".format(who)

ws = MyService(protocols=['restjson', 'restxml'])
application = ws.wsgiapp()
httpd = make_server('localhost', 8000, application)
httpd.serve_forever()

Run this file and point your web browser to http://127.0.0.1:8000/hello.xml?who=John
you should get <result>Hello John !</result> in response.

In this example we have used python's built in webserver which is a good choice when you need to test something out quickly .

For addition i suggest reading How python web frameworks and WSGI fit together

Upvotes: 1

Related Questions