Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight setup to generate Web pages in pure Python

Tags:

python

http

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?

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

like image 348
Nicolas Le Thierry d'Ennequin Avatar asked Jul 07 '11 05:07

Nicolas Le Thierry d'Ennequin


People also ask

Can I use pure Python for web development?

Python can be used to build server-side web applications. While a web framework is not required to build web apps, it's rare that developers would not use existing open source libraries to speed up their progress in getting their application working. Python is not used in a web browser.

Can you make a website with only Python?

Python is a programming language that can be used to create a wide range of different things, including websites. Making websites with Python is simpler than most people believe due to the fact that this language makes use of "frameworks."


3 Answers

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.

like image 138
Flavius Avatar answered Nov 15 '22 21:11

Flavius


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.

like image 24
mac Avatar answered Nov 15 '22 21:11

mac


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.

like image 29
Daniel Roseman Avatar answered Nov 15 '22 20:11

Daniel Roseman