Writing a website in Python

2020-02-16 08:20发布

I'm pretty proficient in PHP, but want to try something new.

I'm also know a bit of Python, enough to do the basics of the basics, but haven't used in a web design type situation.

I've just written this, which works:

#!/usr/bin/python

def main():
    print "Content-type: text/html"
    print
    print "<html><head>"
    print "<title>Hello World from Python</title>"
    print "</head><body>"
    print "Hello World!"
    print "</body></html>"

if __name__ == "__main__":
    main()

Thing is, that this seems pretty cumbersome. Without using something huge like django, what's the best way to write scripts that can process get and post?

标签: python
9条回答
我只想做你的唯一
2楼-- · 2020-02-16 08:26

I really love django and it doesn't seem to me that is huge. It is very powerful but not huge.

If you want to start playing with http and python, the simplest thing is the BaseHttpServer provided in the standard library. see http://docs.python.org/library/basehttpserver.html for details

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-16 08:27

In Python, the way to produce a website is to use a framework. Most of the popular (and actively maintained/supported) frameworks have already been mentioned.

In general, I do not view Djano or Turbogears as "huge", I view them as "complete." Each will let you build a database backed, dynamic website. The preference for one over the other is more about style than it is about features.

Zope on the other hand, does feel "big". Zope is also "enterprise" class in terms of the features that are included out of the box. One very nice thing is that you can use the ZODB (Zope Object Database) without using the rest of Zope.

It would certainly help if we knew what kinds of websites you were interested in developing, as that might help to narrow the suggestions.

查看更多
叼着烟拽天下
4楼-- · 2020-02-16 08:29

There are a couple of web frameworks available in python, that will relieve you from most of the work

  1. Django
  2. Pylons (and the new TurboGears, based on it).
  3. Web2py
  4. CherryPy (and the old TurboGears, based on it)

I do not feel Django as "big" as you say; however, I think that Pylons and CherryPy may be a better answer to your question. CherryPy seems simpler,. but seems also a bit "passé", while Pylons is under active development.
For Pylons, there is also an interesting Pylons book, available online.

查看更多
该账号已被封号
5楼-- · 2020-02-16 08:33

In web2py the previous code would be

in controller default.py:

def main():
    return dict(message="Hello World")

in view default/main.html:

<html><head>
<title>Hello World from Python</title>
</head><body>
{{=message}}
</body></html>

nothing else, no installation, no configuration, you can edit the two files above directly on the web via the admin interface. web2py is based on wsgi but works also with cgi, mod_python, mod_proxy and fastcgi if mod_wsgi is not available.

查看更多
成全新的幸福
6楼-- · 2020-02-16 08:35

Your question was about basic CGI scripting, looking at your example, but it seems like everyone has chosen to answer it with "use my favorite framework". Let's try a different approach.

If you're looking for a direct replacement for what you wrote above (ie. CGI scripting), then you're probably looking for the cgi module. It's a part of the Python standard library. Complimentary functionality is available in urllib and urllib2. You might also be interested in BaseHTTPServer and SimpleHTTPServer, also part of the standard library.

Getting into more interesting territory, wsgiref gives you the basics of a WSGI interface, at which point you probably want to start thinking about more "frameworky" (is that a word?) things like web.py, Django, Pylons, CherryPy, etc, as others have mentioned.

查看更多
▲ chillily
7楼-- · 2020-02-16 08:35

As far as full frameworks go I believe Django is relatively small.

If you really want lightweight, though, check out web.py, CherryPy, Pylons and web2py.

I think the crowd favorite from the above is Pylons, but I am a Django man so I can't say much else.

For more on lightweight Python frameworks, check out this question.

查看更多
登录 后发表回答