Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to create a simple python web service [closed]

I've been using python for years, but I have little experience with python web programming. I'd like to create a very simple web service that exposes some functionality from an existing python script for use within my company. It will likely return the results in csv. What's the quickest way to get something up? If it affects your suggestion, I will likely be adding more functionality to this, down the road.

like image 603
Jeremy Cantrell Avatar asked Jan 06 '09 02:01

Jeremy Cantrell


People also ask

Can Python be used for Web services?

By using Python and REST APIs, you can retrieve, parse, update, and manipulate the data provided by any web service you're interested in.


8 Answers

Have a look at werkzeug. Werkzeug started as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility modules. It includes a powerful debugger, full featured request and response objects, HTTP utilities to handle entity tags, cache control headers, HTTP dates, cookie handling, file uploads, a powerful URL routing system and a bunch of community contributed addon modules.

It includes lots of cool tools to work with http and has the advantage that you can use it with wsgi in different environments (cgi, fcgi, apache/mod_wsgi or with a plain simple python server for debugging).

like image 94
Peter Hoffmann Avatar answered Oct 05 '22 14:10

Peter Hoffmann


web.py is probably the simplest web framework out there. "Bare" CGI is simpler, but you're completely on your own when it comes to making a service that actually does something.

"Hello, World!" according to web.py isn't much longer than an bare CGI version, but it adds URL mapping, HTTP command distinction, and query parameter parsing for free:

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'world'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()
like image 32
Tim Lesher Avatar answered Oct 05 '22 13:10

Tim Lesher


The simplest way to get a Python script online is to use CGI:

#!/usr/bin/python

print "Content-type: text/html"
print

print "<p>Hello world.</p>"

Put that code in a script that lives in your web server CGI directory, make it executable, and run it. The cgi module has a number of useful utilities when you need to accept parameters from the user.

like image 39
Greg Hewgill Avatar answered Oct 05 '22 13:10

Greg Hewgill


Raw CGI is kind of a pain, Django is kind of heavyweight. There are a number of simpler, lighter frameworks about, e.g. CherryPy. It's worth looking around a bit.

like image 20
Charlie Martin Avatar answered Oct 05 '22 14:10

Charlie Martin


Life is simple if you get a good web framework. Web services in Django are easy. Define your model, write view functions that return your CSV documents. Skip the templates.

like image 33
S.Lott Avatar answered Oct 05 '22 14:10

S.Lott


If you mean with "Web Service" something accessed by other Programms SimpleXMLRPCServer might be right for you. It is included with every Python install since Version 2.2.

For Simple human accessible things I usually use Pythons SimpleHTTPServer which also comes with every install. Obviously you also could access SimpleHTTPServer by client programs.

like image 34
max Avatar answered Oct 05 '22 14:10

max


If you mean "web service" in SOAP/WSDL sense, you might want to look at Generating a WSDL using Python and SOAPpy

like image 38
che Avatar answered Oct 05 '22 13:10

che


maybe Twisted http://twistedmatrix.com/trac/

like image 36
mabbit Avatar answered Oct 05 '22 14:10

mabbit