Python: How to create simple web pages without a huge framework? [closed]

北城余情 提交于 2019-12-04 12:21:29

问题


I would like to know if there is a way to create web pages without a huge framework in python.

I think of something like PHP/Apache, which comes just as a language and not with to much overhead (but I don't like PHP...). In PHP there is no ORM, no template engine, etc. But it is very very easy to just print a Hello World to the browser.

I know about Django and really like it, but it is a bit too big for simple web portals (5-10 pages).

I really like something simple, without installing too much.


回答1:


Have you looked up Flask?

It's a much more minimalistic framework, and very easy to set up and get started.




回答2:


I've used Flask (and bottle.py) in the past, but these days I actually prefer Pyramid, from the Pylons folks.

Pyramid is capable of being a large, full-fledged framework, is designed for flexibility, and has no shortage of plugins and extensions available adding additional functionality -- but it also is capable of small, single-file projects; see this tutorial for an example.

Going with Pyramid will give you room to grow if your needs expand over time, while still retaining the ability to start small.




回答3:


Good old CGI is the quickest way to get you started. On most configurations, you just need to drop a python script in 'cgi-bin' and make it executable, no need to install anything. Google for "cgi python", there are lots of tutorials, e.g. this one looks pretty decent.




回答4:


I'm not sure what's wrong with django flatpages for your purposes.

Another alternative would be to replace the django template system with something more powerful, like jinja, so you can write your tag soup and do processing there, with minimal logic in the view.

In practice (given that you already know django), that is likely to be easier than using a microframework (which requires more of the programmer, in exchange for being completely unopinionated about anything).




回答5:


mod_python perhaps?




回答6:


I would recommend CherryPy

import cherrypy
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())



回答7:


Sure, you can go really lean with the CGI or wsgiref route. However, you get what you pay for, and I prefer Flask or WerkZeug for all the pain they prevent.

From wsgiref python docs:

from wsgiref.simple_server import make_server

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')] # HTTP Headers
    start_response(status, headers)
    return ["Hello World"]

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

# Serve until process is killed
httpd.serve_forever()



回答8:


Python works well using CGI.

that's the simplest thing you can do: it only needs apache and a working python environment, and is the closest to a standard php setup.

remember that, when using CGI, your python script is responsible for outputting the necessary HTTP headers (sys.stdout.write('Content-Type: text/html\n\n')), but there is a CGI module which is part of the python standard library which greatly helps dealing with the raw stuffs (post/get arguments parsing, header retrieval, header generation).



来源:https://stackoverflow.com/questions/9979326/python-how-to-create-simple-web-pages-without-a-huge-framework

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