开发者

Lightweight setup to generate Web pages in pure Python

I'm totally new to Python and I'd like to start building Web pages with it (but not using a Web framework or a templating module). What are the minimum requirements? Could you suggest a simple setup?

Than开发者_运维技巧k you!

EDIT: I'm not trying to be minimalist at all cost. What I'm looking for is a simple, common solution, that stays close to the language (and doesn't impose a design paradigm, eg MVC).


Clean WSGI app, without a fully-fledged framework:

from wsgiref.simple_server import make_server

def application(environ, start_response):

   # Sorting and stringifying the environment key, value pairs
   response_body = ['%s: %s' % (key, value)
                    for key, value in sorted(environ.items())]
   response_body = '\n'.join(response_body)

   status = '200 OK'
   response_headers = [('Content-Type', 'text/plain'),
                  ('Content-Length', str(len(response_body)))]
   start_response(status, response_headers)

   return [response_body]

# Instantiate the WSGI server.
# It will receive the request, pass it to the application
# and send the application's response to the client
httpd = make_server(
   'localhost', # The host name.
   8051, # A port number where to wait for the request.
   application # Our application object name, in this case a function.
   )

# Wait for a single request, serve it and quit.
httpd.handle_request()

Then you can use nginx: http://wiki.nginx.org/NgxWSGIModule

This is the most stable, safe and bare-to-bone setup.

More examples at: https://bitbucket.org/lifeeth/mod_wsgi/src/6975f0ec7eeb/examples/.

This is the best way to learn (as you asked). I've gone this path already.


I go with the flow, and I would recommend to use a lightweight framework.

For one, web apps expose your server to security risks, so it's good to use something that is maintained by a more-or-less large community of developers (more eyeballs to fix vulnerabilities).

Also if you want to "stay close to the language", you need some sort of abstraction layer to manage HTTP in a pythonic way. Python is all about high-levelness [battery included].

Some of the framework stay really close to python's syntax, semantics and style. Take a look at webpy for example. I think this quote says much of the philosophy behind webpy:

"Django lets you write web apps in Django. TurboGears lets you write web apps in TurboGears. Web.py lets you write web apps in Python." -- Adam Atlas

Another good candidate in terms of conciseness and use of "regular" python is cherrypy. From their website:

CherryPy allows developers to build web applications in much the same way they would build any other object-oriented Python program. [...] Your CherryPy powered web applications are in fact stand-alone Python applications embedding their own multi-threaded web server. You can deploy them anywhere you can run Python applications.


If you really want to to this, the minimum you need is an understanding of WSGI, which is the glue between Python and the web server. You can build a Python web app directly on top of this.

But, like the other answerers, I really would encourage you to use a framework. They're not all huge monolithic things like Django - see for example microframeworks like Flask. They'll take care of the obvious things like routing URL requests to the right code.


Just run this command

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

doesn't get much more lightweight than that

now look at CGIHTTPServer.py in the Python library for more ideas

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜