简体   繁体   English

我对 python 网络应用程序感到困惑

[英]I'm confused about python web application

我刚刚学习了“python”编程以及有关 css 和 html 以及 java 脚本的基本知识,我之前是“php”程序员,我很困惑,是否可以仅在 python(服务器端)中编写 Web 应用程序使用框架(django、flask 等)或者如果我使用 django 开发,是否可以将它们(django 和 pyhotn)放在一个程序中

Django is built using Python therefore you'd not be able to build your Django web application without Python. Django 是使用 Python 构建的,因此如果没有 Python,您将无法构建 Django Web 应用程序。

I suggest you to begin with the Django Getting Started checklist to see if it is something that you're looking for.我建议您从Django 入门清单开始,看看它是否是您正在寻找的东西。

If you mean, you want to serve a web application and do something else in the very same process, sure.如果您的意思是,您想要为 Web 应用程序提供服务并在相同的过程中执行其他操作,当然可以。 Start a thread and make your second thing-to-do a background thread.启动一个线程并使您的第二件事成为后台线程。 I'd consider it as a bad practice though.不过,我认为这是一种不好的做法。 But you can try.不过你可以试试。

Otherwise, Django is Python.否则,Django 就是 Python。

And yes, theoretically, you can write a web application without Django or any framework.是的,理论上,您可以在没有 Django 或任何框架的情况下编写 Web 应用程序。 But you would need to write huge amounts of code.但是您需要编写大量代码。 I don't think you're asking the question in this way but, just for making it clear.我不认为你是这样问这个问题的,只是为了说清楚。

You can write a web application the way you're used to in PHP using Python by using the cgi module.通过使用 cgi 模块,您可以按照在 PHP 中使用 Python 的方式编写 Web 应用程序。

However, while cgi style web application is simple for one page application, this is considered very poor style in Python, it doesn't scale, it's somewhat weird and tedious (because it abuses stdin/stdout), and it's bad for performance (because it has to reload all modules every request).然而,虽然 cgi 风格的 web 应用程序对于单页应用程序来说很简单,但这在 Python 中被认为是非常糟糕的风格,它不能扩展,有点奇怪和乏味(因为它滥用 stdin/stdout),而且对性能不利(因为它必须重新加载每个请求的所有模块)。 This style of writing application doesn't scale to larger, more complicated projects, although this is the only style that is really available in PHP, but it is partly the reason why most major PHP frameworks almost always advise you to write .htaccess rules that rewrites all request to a single file.这种编写应用程序的风格不能扩展到更大、更复杂的项目,尽管这是 PHP 中唯一真正可用的风格,但这也是大多数主要 PHP 框架几乎总是建议您编写 .htaccess 规则的部分原因将所有请求重写为单个文件。 The same problems plagues PHP, however in PHP, cgi style application is somewhat less painful because the language is optimised for this style of application, but it is also the reason why PHP have had to preload thousands of built in functions in the global namespace;同样的问题也困扰着 PHP,但是在 PHP 中,cgi 风格的应用程序并不那么痛苦,因为语言针对这种风格的应用程序进行了优化,但这也是 PHP 不得不在全局命名空间中预加载数千个内置函数的原因; as the language would be unbearable to use otherwise.因为否则语言将无法忍受。

In Python, it's more common to use WSGI to talk to the webserver, but using WSGI is much more complicated than CGI, as WSGI is very low level and is primarily intended for framework developer rather than application developer, and that WSGI requires you to keep a persistent process running (this is in contrast to the new process for every request style of CGI).在 Python 中,使用 WSGI 与 Web 服务器通信更为常见,但使用 WSGI 比 CGI 复杂得多,因为 WSGI 的级别非常低,主要面向框架开发人员而不是应用程序开发人员,而且 WSGI 要求您保持一个持续运行的进程(这与 CGI 的每个请求样式的新进程形成对比)。 Most application developers are expected to instead use a web framework, as Python has a lot of frameworks suited for many different projects and sizes of applications.大多数应用程序开发人员都希望改用 Web 框架,因为 Python 有许多适合于许多不同项目和应用程序大小的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM