Python: How to create simple web pages without a h

2020-06-03 06:42发布

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.

8条回答
对你真心纯属浪费
2楼-- · 2020-06-03 07:20

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

查看更多
乱世女痞
3楼-- · 2020-06-03 07:26

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楼-- · 2020-06-03 07:28

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()
查看更多
够拽才男人
5楼-- · 2020-06-03 07:29

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.

查看更多
该账号已被封号
6楼-- · 2020-06-03 07:35

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

查看更多
一纸荒年 Trace。
7楼-- · 2020-06-03 07:36
登录 后发表回答