我正在尝试使用 Django 框架创建网站。我查看了 Django 项目站点上的教程,但其中包含很多我不需要的信息。我有提供输出的 python 脚本,我需要在 web 上有这个输出。我的问题是如何简单地管理 Django 以获得启动脚本并在网络上提供其输出的链接,或者您提供我可以阅读的链接?
谢谢。
最佳答案
"I have python scripts which provides output and I need to have this output on the web."
这不是 Django 的目的。你想做的事情可以通过像这样简单的事情来实现:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("magic content goes here")
if __name__=="__main__":
try:
server = HTTPServer(("", 8080), Handler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
观察 self.wfile.write
行。无论你在那里写什么,最终都会出现在浏览器中。如果重要,您可以在 Handler
中使用 self.path
来检查请求了哪个文件。
使用 Python 2.6.4 测试,使用 Chrome 浏览器访问服务器。
关于Python Django 简单站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2228966/