如何在网页中运行python脚本 [英] How to run python script in webpage

查看:242
本文介绍了如何在网页中运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 python 很陌生.只知道什么是python.我创建了以下代码(在 python IDLE 中):

I'm very new to python. Just know what is python. I have created the below code (In python IDLE):

print "Hi Welcome to Python test page\n";
print "Now it will show a calculation";
print "30+2="; print 30+2;

然后我将此页面保存在我的本地主机中作为 index.py

Then I saved this page in my localhost as index.py

我运行脚本使用http://localhost/index.py

但它没有显示执行的python脚本.相反,它将上述代码显示为 HTML.问题出在哪儿?请有人告诉我如何在网页中运行python?

But it not showed executed python script. instead it showed the above code as HTML. Where is the problem? please anyone tell me how to run python in webpage?

推荐答案

为了让你的代码显示出来,你需要做几件事:

In order for your code to show, you need several things:

首先,需要有一个服务器来处理 HTTP 请求.目前,您只是在本地硬盘驱动器上使用 Firefox 打开一个文件.需要像 Apache 或类似的服务器.

Firstly, there needs to be a server that handles HTTP requests. At the moment you are just opening a file with Firefox on your local hard drive. A server like Apache or something similar is required.

其次,假设您现在有一个提供文件的服务器,您还需要将代码解释为服务器的 Python 代码的东西.对于 Python 用户,现在的解决方案是 mod_wsgi.但是对于更简单的情况,您可以坚持使用 CGI(更多信息 此处),但如果您想轻松生成网页,则应使用现有的 Python 网络框架,例如 Django.

Secondly, presuming that you now have a server that serves the files, you will also need something that interprets the code as Python code for the server. For Python users the go to solution is nowadays mod_wsgi. But for simpler cases you could stick with CGI (more info here), but if you want to produce web pages easily, you should go with a existing Python web framework like Django.

设置它可能很麻烦,所以要做好准备.

Setting this up can be quite the hassle, so be prepared.

这篇关于如何在网页中运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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