Python Web编程 [英] Python web programming

查看:42
本文介绍了Python Web编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好.

正如标题所示,我对使用python进行Web开发有一些疑问.

  • 什么是开发环境的最佳设置,更具体地说,是要使用的Web服务器,如何将python与之绑定.最好,我希望它可以在* nix和win环境中实现.

我上次尝试使用apache + mod_python + CherryPy时的主要问题是必须重新加载Web服务器才能查看更改.被认为是正常的吗?由于某些原因,cherrypy的自动重载根本无法正常工作.

  • 将有效的Python应用程序部署到生产环境的最佳设置是什么?为什么?我现在在我的PHP Web应用程序中使用lighttpd,但是与nginx相比,它对python的效果如何?

  • 是否值得直接使用框架潜水或推出自己的简单东西?我看到Django吸引了很多粉丝,但我认为这对于我的需求而言可能会过高,所以我开始研究CherryPy.

  • 如果必须重新加载httpd来查看更改,Python应用程序将如何提供服务?诸如产生子进程的永久进程之类的东西,其中所有主要文件都包括在服务器启动时发生,然后才懒加载所需资源?

  • Python支持多线程,在开发Web应用程序时是否需要考虑使用多线程技术?那有什么好处?在什么情况下?

非常感谢!

解决方案

所以这是我的想法:

我正在使用 Python Paste 来开发我的应用程序并最终也运行它(或其他任何python网络服务器).我通常不使用mod_python或mod_wsgi,因为它会使开发设置更加复杂.

我正在使用 zc.buildout 一起管理我的开发环境和所有依赖项与 virtualenv .这给了我一个孤立的沙箱,它不会干扰系统范围内安装的任何Python模块.

对于部署,我还使用buildout/virtualenv,最终使用不同的buildout.cfg.我还使用粘贴部署,它是一种配置机制,其中有用于开发和部署的不同配置文件.

因为我通常是独立运行粘贴/樱桃等,所以我正在使用Apache,NGINX或仅在其前面使用Varnish.这取决于您需要的配置选项.例如.如果不需要虚拟主机,重写规则等,那么我不需要前面的全功能Web服务器.使用网络服务器时,我通常使用ProxyPass或使用mod_rewrite进行的更复杂的重写.

我目前使用的Python网络框架是 repoze.bfg 现在顺便说一句./p>

关于您有关重新加载的问题,例如,使用mod_python,但是使用独立的"paster服务... -reload"等时,到目前为止效果很好.repoze.bfg另外具有一些设置,用于在更改模板时自动重新加载模板.如果您使用的框架有文档,则应该记录下来.

至于通常在python Web服务器内部使用的多线程.由于CherryPy支持此功能,我想您不必为此担心,它应该自动使用.您最终应该只是做一些基准测试,以找出您的应用程序在哪种线程上表现最佳.

希望有帮助.

Good morning.

As the title indicates, I've got some questions about using python for web development.

  • What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win environment.

My major concern when I last tried apache + mod_python + CherryPy was having to reload webserver to see the changes. Is it considered normal? For some reason cherrypy's autoreload didn't work at all.

  • What is the best setup to deploy a working Python app to production and why? I'm now using lighttpd for my PHP web apps, but how would it do for python compared to nginx for example?

  • Is it worth diving straight with a framework or to roll something simple of my own? I see that Django has got quite a lot of fans, but I'm thinking it would be overkill for my needs, so I've started looking into CherryPy.

  • How exactly are Python apps served if I have to reload httpd to see the changes? Something like a permanent process spawning child processes, with all the major file includes happening on server start and then just lazy loading needed resources?

  • Python supports multithreading, do I need to look into using that for a benefit when developing web apps? What would be that benefit and in what situations?

Big thanks!

解决方案

So here are my thoughts about it:

I am using Python Paste for developing my app and eventually also running it (or any other python web server). I am usually not using mod_python or mod_wsgi as it makes development setup more complex.

I am using zc.buildout for managing my development environment and all dependencies together with virtualenv. This gives me an isolated sandbox which does not interfere with any Python modules installed system wide.

For deployment I am also using buildout/virtualenv, eventually with a different buildout.cfg. I am also using Paste Deploy and it's configuration mechanism where I have different config files for development and deployment.

As I am usually running paste/cherrypy etc. standalone I am using Apache, NGINX or maybe just a Varnish alone in front of it. It depends on what configuration options you need. E.g. if no virtual hosting, rewrite rules etc. are needed, then I don't need a full featured web server in front. When using a web server I usually use ProxyPass or some more complex rewriting using mod_rewrite.

The Python web framework I use at the moment is repoze.bfg right now btw.

As for your questions about reloading I know about these problems when running it with e.g. mod_python but when using a standalone "paster serve ... -reload" etc. it so far works really well. repoze.bfg additionally has some setting for automatically reloading templates when they change. If the framework you use has that should be documented.

As for multithreading that's usually used then inside the python web server. As CherryPy supports this I guess you don't have to worry about that, it should be used automatically. You should just eventually make some benchmarks to find out under what number of threads your application performs the best.

Hope that helps.

这篇关于Python Web编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆