如何使用 python 作为服务器端语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40943107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use python as a server-side language?
提问by Barsik the Cat
I am trying to use Python instead of PHP (just for educational purposes). I am testing my webpages on XAMPP and I already added python and cgi to configs. I managed to make a very basic webpage with this code
我正在尝试使用 Python 而不是 PHP(仅用于教育目的)。我正在 XAMPP 上测试我的网页,并且我已经将 python 和 cgi 添加到配置中。我设法用这段代码制作了一个非常基本的网页
#!/Python/python
print("Content-Type: text/plain;charset=utf-8")
print()
print("Hello World!")
Though, this is it. I can't find any info on how exactly do I serve webpages with Python 3+. Most of the info is outdated or controversial. Is there an up-to-date guide on how to use Python as a server-side language?
虽然,就是这样。我找不到有关如何使用 Python 3+ 提供网页的任何信息。大多数信息已经过时或有争议。是否有关于如何使用 Python 作为服务器端语言的最新指南?
回答by Blanen
What Nicarus said was a valid sugestion but I also recommend some other things.
Nicarus 所说的是一个有效的建议,但我也推荐一些其他的东西。
First, for your development environment you won't need to use xampp or wamp or such things, python has it's own HTTP server, although not the simplest thing to use, the libraries and frameworks I will explain next use that.
首先,对于您的开发环境,您不需要使用 xampp 或 wamp 之类的东西,python 有它自己的 HTTP 服务器,虽然不是最简单的使用,但我将在下次使用时解释库和框架。
So, most python web developers don't use raw python to use python as their programming language for the web. Most developers use a framework or library of some kind. These frameworks range from being rather heavy and opinionated, like Django, to smaller ones like Flask. Most, if not all, of these frameworks provide some kind of easy and quick way to set up a development HTTP server for testing.
因此,大多数 python 网络开发人员不使用原始 python 来使用 python 作为他们的网络编程语言。大多数开发人员使用某种框架或库。这些框架从笨重和固执己见的 Django 到较小的 Flask。大多数(如果不是全部)这些框架都提供了某种简单快捷的方法来设置开发 HTTP 服务器以进行测试。
I would recommend looking up Django first since it has the most comprehensive tutorials and guides to get you started. Then, ones you're more comfortable in the Python language you can fairly easily use something else with less hand holding.
我建议先查找 Django,因为它有最全面的教程和指南来帮助您入门。然后,那些你更熟悉 Python 语言的你可以很容易地使用其他的东西,而更少的手。
回答by Bernard
For the purposes you mention, this current setup should work (at least for a while):
出于您提到的目的,此当前设置应该可以工作(至少在一段时间内):
Apache Setup
阿帕奇设置
Changes in httpd.conf
httpd.conf 中的更改
- Change deniedto granted
- 拒绝更改为授予
<Directory />
AllowOverride none
Require all granted
</Directory>
<Directory />
AllowOverride none
Require all granted
</Directory>
- Look for Options +Indexes +FollowSynsLinks........ and add +ExecCGIto that line.
- 查找 Options +Indexes +FollowSynsLinks........ 并将 + ExecCGI添加到该行。
Options +Indexes +FollowSymLinks +Multiviews +ExecCGI
Options +Indexes +FollowSymLinks +Multiviews +ExecCGI
- Look for AddHandler cgi-script .cgi and add .pyto it
- 查找 AddHandler cgi-script .cgi 并将.py添加到它
AddHandler cgi-script .cgi .py
AddHandler cgi-script .cgi .py
Changes in httpd-vhosts.conf
httpd-vhosts.conf 中的更改
<Directory "{Your directory}"/>
Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI
AllowOverride All
Require all granted
</Directory>
<Directory "{Your directory}"/>
Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI
AllowOverride All
Require all granted
</Directory>
example.py
例子.py
First line of Python code
#!C:\{somepath}\python
第一行 Python 代码
#!C:\{somepath}\python
You need to find the right path and bar convention( /, \, //...) for the first and commented line of code. In your example is: #!/Python/python
您需要为第一行和注释的代码行找到正确的路径和条形约定(/、\、//...)。在你的例子中是:#!/Python/python
Run this script.py to find it
运行这个 script.py 来找到它
import sys
print(sys.executable)
import sys
print(sys.executable)
Paste the output next to #!
and you should be able to test your test-code.py into your **www\ folder
If happens to be the case that it does not work, try different combinations of python, python.exe with different bar conventions like "/" "//" "\" next to the #!
line.
粘贴旁边的输出#!
,您应该可以将您的 test-code.py 测试到您的 **www\ 文件夹如果碰巧它不起作用,请尝试使用不同栏的 python、python.exe 的不同组合#!
行旁边的诸如“/”“//”“\”之类的约定。
To display an HTML file add
print ('Content-type: text/html\n\n')
on to your .py code.**
要显示 HTML 文件,请添加
print ('Content-type: text/html\n\n')
到您的 .py 代码中。**
Full code
Note fon html_content = f'''<html>...
完整的代码
注释˚F上html_content = f'''<html>...
#!C:\Program Files (x86)\Python37-32\python
# -*- coding: utf-8 -*-
import cgi
stringvar = 'Hello variable'
intvar = 30
html_content = f'''<html>
<head><title>My first Python CGI app</title></head>
<body>
<p>Hello, 'world'!</p>
<p>This is a stringvar = {stringvar} </p>
<p>This is intvar = {intvar} </p>
</body>
</html>'''
print ('Content-type: text/html\n\n')
print (html_content)
"Most of the info is outdated or controversial"
“大部分信息已经过时或有争议”
Totally agree. I hope this works!
完全同意。我希望这有效!
回答by fernandezr
Python can be a great side server language, but not in the way PHP is. It is highly recommended to use a framework, like flask.
Python 可以是一种很棒的端服务器语言,但不像 PHP 那样。强烈建议使用框架,例如flask。
In PHP you have different .php files (like index.php
) that do a basic routing, but in python (with Flask and also some others frameworks) you must define the URI path within the function.
在 PHP 中,您有不同的 .php 文件(如index.php
)来执行基本路由,但在 python(使用 Flask 和其他一些框架)中,您必须在函数中定义 URI 路径。
You can see here an example, from its webpage Flask
你可以在这里看到一个例子,来自它的网页Flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
And if you look for a API REST framework, take a look to Falcon
如果您正在寻找 API REST 框架,请查看Falcon