我是Python的新手,我想开始用它构建Web页面(但不使用Web框架或模板模块).最低要求是什么?你能建议一个简单的设置吗?
谢谢!
编辑:我不是不惜一切代价都是极简主义者.我正在寻找的是一个简单,通用的解决方案,与语言保持接近(并没有强加设计范例,例如MVC).
干净的 WSGI 应用程序,没有成熟的框架:
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()
Run Code Online (Sandbox Code Playgroud)
然后你可以使用nginx: http: //wiki.nginx.org/NgxWSGIModule
这是最稳定、最安全、最简单的设置。
更多示例请参见:https://bitbucket.org/lifeeth/mod_wsgi/src/6975f0ec7eeb/examples/。
这是最好的学习方式(正如您所要求的)。我已经走了这条路。
我随波逐流,我建议使用轻量级框架。
其一,网络应用程序会使您的服务器面临安全风险,因此最好使用由或多或少大型开发人员社区维护的东西(更多的眼球来修复漏洞)。
另外,如果你想“与语言保持密切联系”,你需要某种抽象层以Pythonic方式管理HTTP 。Python 的核心是高水平[包括电池]。
一些框架非常接近 python 的语法、语义和风格。以webpy为例。我认为这句话说明了 webpy 背后的大部分哲学:
“Django 让您可以在 Django 中编写 Web 应用程序。TurboGears 让您可以在 TurboGears 中编写 Web 应用程序。Web.py 让您可以用 Python 编写 Web 应用程序。” ——亚当·阿特拉斯
就“常规”python 的简洁性和使用而言,另一个不错的候选者是cherrypy。从他们的网站:
CherryPy 允许开发人员以与构建任何其他面向对象的 Python 程序大致相同的方式构建 Web 应用程序。[...] CherryPy 支持的 Web 应用程序实际上是嵌入自己的多线程 Web 服务器的独立 Python 应用程序。您可以将它们部署在任何可以运行 Python 应用程序的地方。
归档时间: |
|
查看次数: |
1910 次 |
最近记录: |