142

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.

Jeremy Cantrell
  • 26,392
  • 13
  • 55
  • 78
  • This shows a nice quick sample: http://www.dreamsyssoft.com/blog/blog.php?/archives/6-Create-a-simple-REST-web-service-with-Python.html – Rocky Pulley Jul 26 '13 at 18:00
  • https://github.com/pramttl/webipy I wrote this tool which automatically transforms your python functions into web endpoints. It has some restrictions on your function definitions, but is by far the quickest way to generate web endpoints for your python functions. Infact webipy uses django, but it automatically generates django views required for all your python functions. You do not have to write any "web code". – Pranjal Mittal Nov 25 '14 at 12:01

9 Answers9

56

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).

Peter Hoffmann
  • 56,376
  • 15
  • 76
  • 59
  • 2
    I ended up using werkzeug for this. I love how flexible it is. Thanks for the recommendation. – Jeremy Cantrell Apr 02 '09 at 21:11
  • 1
    I tried about three other web frameworks before I got to this one. This is the first one I could get working out of the box. Great rec! – BenDundee Apr 03 '15 at 13:52
28

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()
Tim Lesher
  • 6,341
  • 2
  • 28
  • 42
  • Ah yes, web.py seems great. As of now, the Web site says: 500 - Internal Server Error Otherwise, our code does not work for me: ... app = web.application(urls, globals()) AttributeError: 'module' object has no attribute 'application' – bortzmeyer Jan 06 '09 at 09:32
  • 1
    The web site is back. You might want to check your version, as Tim's code looks right. – Charlie Martin Jan 06 '09 at 19:57
  • 1
    I ran into this too. If you are running Ubuntu, the version that ships in the 8.10 repository is quite out-of-date. Grab the newest from the website or use easy_install and you should be golden. – bouvard Jan 06 '09 at 20:55
  • according to webpy.org (the official web.py website) yandex is using web.py (they have 70 million page views/day).. so it must be efficient.. – programmer Mar 15 '11 at 19:47
  • This is exactly what I needed. Simple, straight to the point and easy to use. – GuiSim Dec 09 '12 at 18:24
17

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
12

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.

bluskies
  • 29
  • 8
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
9

Look at the WSGI reference implementation. You already have it in your Python libraries. It's quite simple.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • Like I said, my experience with python web programming is pretty limited, but a WSGI app can be run "standalone", without a web server like apache, right? – Jeremy Cantrell Jan 06 '09 at 15:30
  • Yes. Totally stand-alone. Works great. You can't use port 80, however, without Apache or special privileges -- but that's an OS security issue. – S.Lott Jan 06 '09 at 15:50
4

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.

max
  • 29,122
  • 12
  • 52
  • 79
2

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
2

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

Community
  • 1
  • 1
che
  • 12,097
  • 7
  • 42
  • 71
  • I just mean "web service" in the most informal sense. Providing data via http that will be consumed by some other app. As I said in the question, it will likely be csv. – Jeremy Cantrell Jan 06 '09 at 15:24
  • WSDL would be probably the most standards based...and that would be for being consumed... – Thufir Jan 16 '17 at 22:11
1

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

mabbit
  • 187
  • 3
  • I don't have anything against Twisted, but this is exactly what I wanted to stay away from (heavy dependencies). – Jeremy Cantrell Jan 06 '09 at 15:25
  • @Jeremy: Twisted is not heavy. – nosklo Jan 06 '09 at 17:49
  • i agree it can be a bit daunting at first glance, but im not sure what you mean by 'heavy dependencies'. It uses packages from standard python as far as I know. There are a lot of moving parts inside the module but for simple things you don't need to do that much. – mabbit Jan 08 '09 at 03:48