gpt4 book ai didi

python - 在 Cloud9 上运行 Python CGI

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:03 24 4
gpt4 key购买 nike

我可以让 Python CGI 程序在其他环境中运行,但希望帮助我在 Cloud9 环境中进入“hello world”。

我的简单程序运行良好,但我什至无法获得简单程序的 HTML/Web 版本。

这次运行给了我以下建议:您的代码正在 https://workspacename-username.c9.io 运行.
重要提示:在脚本中使用 os.getenv(PORT, 8080) 作为端口,使用 os.getenv(IP, 0.0.0.0) 作为主机!

但我不知道该怎么办。

我的简单代码如下。我还尝试将其作为 filename.cgi 运行,但这不起作用。

#!/usr/bin/env python
print "Content-Type: text/html"
print "<html>"
print "<head><title>My first CGI program</title></head>"
print "<body>"
print "<p> It works!! </p>"
print "</body></html>"

最佳答案

需要先安装Flask,可以通过命令来完成:

$ sudo easy_install Flask

你的代码应该是这样的:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
html= "<html>"
html+= "<head><title>My first CGI program</title></head>"
html+= "<body>"
html+= "<p> It works!! </p>"
html+= "</body></html>"
return html

app.run(host = os.getenv('IP','0.0.0.0'), port=int(os.getenv('PORT',8080)))

启动应用程序后,您会看到

Your code is running at https://workspacename-username.c9users.io.
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

现在您可以打开https://workspacename-username.c9users.io ,它会起作用

关于python - 在 Cloud9 上运行 Python CGI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com