Recommended python library/framework for local web app?

我怕爱的太早我们不能终老 提交于 2019-11-30 08:41:15

I think web2py might be a geat solution here. It requires no installation and has no dependencies (it even comes with its own Python interpreter as well as a web-server and the SQLite database). You can even distribute your application as a Windows or Mac binary (including web2py), and users can easily run it locally with no installation.

Bottle is a very lightweight micro-framework. It comes as a single .py-file with no external dependencies, supports routing, a small template-engine and comes with an integrated webserver. It is easy to use and slim.

This sounds like a perfect match to your requirements :)

I've used BaseHTTPServer for this purpose. It's a web server built in to the Python standard library, and lets you have full control over the content you deliver.

Since it's part of Python's standard library, you don't have to worry about any platform-specific configuration. I've used the same local server script on a Windows, Linux, and Mac OS X system without modification.

A sample bit of code might be:

import BaseHTTPServer

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("Hello world!")

server_address = ('', 8080)
httpd = BaseHTTPServer.HTTPServer(server_address, Handler)
httpd.serve_forever()

I have written a handful of such "local web server" apps since I asked this question. I don't have a final "which framework is best" answer, but I do have some insights:

  1. A simple or micro-framework is indeed a good choice.
  2. I've tried CherryPy and Flask frameworks. Flask is the clear winner for simplicity, with basic "set up some AJAX serving pages" functions entirely trivial to write in Flask. CherryPy documentation is often opaque, and its setup complexity notably higher.
  3. I'm happy with Flask, but I continue to look around. I would especially like to try Bottle, which I've seen reviewed very highly, including in other StackOverflow discussions and this side-by-side comparison: http://www.slideshare.net/r1chardj0n3s/web-microframework-battle web2py also looks worth a try.

A very simple server in the standard library is wsgiref.simple_server.

The example looks trivial (demo_app is also part of the module):

from wsgiref.simple_server import make_server, demo_app

httpd = make_server('', 8000, demo_app)
print("Serving HTTP on port 8000...")

# Respond to requests until process is killed
httpd.serve_forever()
Don O'Donnell

I have no direct experience but I've heard some good things about web2py:

Django vs web2py for a beginner developer

Pylons is extremely easy to use once you get some simple configuration set up, you'll have to have a good idea of what you want though.

Django comes with a built-in web server that allows you to fully test your application locally (via localhost:8080 or something of the sort). As a matter of fact, I've used it more than once to run a complete web-application locally prior to deploying it to a server. I see no reason you can't use it for your own local web-app purposes. Although it may seem that Django is big and complex, this solution is self-contained and simple to run:

  1. Install Django
  2. Go through the great tutorial, which very soon shows you how to run the web-server
  3. Write your code

That's about it. Deploying it to other machines is also simple to do, especially with something like virtualenv.

If you don't want a large web-framework at all, I'll have to join Greg's advice to use BaseHTTPServer. I've used it before for specialized local applications and it's working well, doing what's expected from it and not much more. It's a very flexible solution allowing you to build something quite custom if you need it.

chances are, you want an admin interface for basic CRUD operations on some database tables. Then Django is your best choice.

Any framework will do this. Django certainly will do, but since you want something smaller, I'd recommend will BFG/Pyramid, which is very lightweight, extremely extensible and flexible and fun to use. But there are loads of others, and as mentioned, the built in wsgiref is as lightweight as you get. :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!