I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).
我有一个连续运行的python脚本。它每30秒输出2行信息。我希望能够在网上查看此输出。特别是,我希望网站能够自动更新(每30秒在页面/网站顶部添加新输出,而不必刷新页面)。
I understand I can do this with javascript but is there a python only based solution? Even if there is, is javascript the way to go? I'm more than willing to learn javascript if needed but if not, I'd like to stay focused on python.
我知道我可以用javascript做到这一点但是有一个基于python的解决方案吗?即使有,是javascript的方式去?如果需要,我非常愿意学习javascript,但如果没有,我想继续专注于python。
Sorry for the basic question but I'm still clueless when it comes to web programming.
对不起基本问题,但在网络编程方面,我仍然无能为力。
Thx!
9 个解决方案
#1
This question appears to have two things in it.
这个问题似乎有两件事。
-
Presentation on the web. This is easy to do in Python -- use Django or TurboGears or any Python-based web framework.
在网上发表演讲。这在Python中很容易 - 使用Django或TurboGears或任何基于Python的Web框架。
-
Refresh of the web page to show new data. This can be done two ways.
刷新网页以显示新数据。这可以通过两种方式完成。
-
Some fancy Javascript to refresh.
一些花哨的Javascript刷新。
-
Some fancy HTML to refresh the page. The meta refresh tag is what you want. If you do this, you have an all-Python solution.
一些花哨的HTML来刷新页面。元刷新标记是您想要的。如果你这样做,你有一个全Python解决方案。
-
#2
If you want a dead simple way to print data from a Python script to a webpage and update automatically, you can just print from the script. For example, using Apache with the below Python CGI script:
如果您想要一种简单的方法将数据从Python脚本打印到网页并自动更新,您只需从脚本中打印即可。例如,将Apache与下面的Python CGI脚本一起使用:
#!/usr/bin/python
import time
import sys
import random
def write(inline=''):
sys.stdout.write(inline)
sys.stdout.write('\r\n')
sys.stdout.flush()
#prints out random digits between 1 and 1000 indefinitely
write("Content-type: text/html\r\n")
i = 0
while(True):
i = i + 1
time.sleep(1)
write(str(i) + "<br />")
If I navigate to that in a browser (Firefox, don't know if other browsers might work differently with regards to buffering etc), it prints the digits continually. Mind you, it prints in sequential order so the newer data is at the bottom rather than that top, but it might work depending on what exactly you're looking to do.
如果我在浏览器中导航到它(Firefox,不知道其他浏览器在缓冲方面是否可能有不同的工作方式),它会不断打印数字。请注意,它按顺序打印,因此较新的数据位于底部而不是顶部,但它可能会起作用取决于您想要做什么。
If this isn't really what you're looking for, the only other way to do this is an automatic refreshing page (either in an iframe, or the whole page) or with javascript to do the data fetching.
如果这不是您真正想要的,那么唯一的另一种方法是自动刷新页面(在iframe中,或在整个页面中)或使用javascript来进行数据获取。
You can use a meta refresh tag in your iframe or page HTML source, and your CGI can print the new data each time it's refreshed. Alternatively, you can use javascript with an XMLHTTPRequest to read the new data in without a visual page refresh.
您可以在iframe或页面HTML源中使用元刷新标记,并且每次刷新时,CGI都可以打印新数据。或者,您可以使用带有XMLHTTPRequest的javascript来读取新数据,而无需可视化页面刷新。
#3
You could use Comet, but I strongly discourage you from doing so. I'd just write a short Javascript, using jQuery this is really straightforward.
你可以使用Comet,但我强烈反对你这样做。我只是写一个简短的Javascript,使用jQuery这真的很简单。
Another possibility is the use of an iframe that reloads every 30 seconds, this would prevent the whole page from reloading.
另一种可能性是使用每30秒重新加载的iframe,这将阻止整个页面重新加载。
#4
If you want to do it entirely in python you can use pyjamas.
如果你想完全在python中完成它,你可以使用睡衣。
It generates javascript directly from python code, so you avoid writing javascript yourself completely.
它直接从python代码生成javascript,因此您可以避免完全自己编写javascript。
#5
JavaScript is the primary way to add this sort of interactivity to a website. You can make the back-end Python, but the client will have to use JavaScript AJAX calls to update the page. Python doesn't run in the browser, so you're out of luck if you want to use just Python.
JavaScript是向网站添加此类交互性的主要方式。您可以制作后端Python,但客户端必须使用JavaScript AJAX调用来更新页面。 Python不能在浏览器中运行,所以如果你只想使用Python,那就太不走运了。
(It's also possible to use Flash or Java applets, but that's a pretty heavyweight solution for what seems like a small problem.)
(它也可以使用Flash或Java小程序,但对于看似小问题的东西来说,这是一个非常重要的解决方案。)
#6
You need Javascript in one way or another for your 30 second refresh. Alternatively, you could set a meta tag refresh for every 30 seconds to redirect to the current page, but the Javascript route will prevent page flicker.
您需要以某种方式使用Javascript进行30秒刷新。或者,您可以设置元标记刷新每30秒重定向到当前页面,但Javascript路由将阻止页面闪烁。
#7
Write your output to a log file, and load the log file to the browser thru web server. If you need auto refresh, create a template HTML file with tag to refresh every 15 seconds:
将输出写入日志文件,并通过Web服务器将日志文件加载到浏览器。如果需要自动刷新,请创建一个带有标记的模板HTML文件,每15秒刷新一次:
<META HTTP-EQUIV="refresh" CONTENT="15">
and use server side include to include the log file on the page.
并使用服务器端include包括页面上的日志文件。
#8
Perhaps "long polling" is what you're looking for?
也许“长期民意调查”正是你要找的?
Long polling could be described as "HTTP push", basically you have a (Python) script served via a web-server, which only outputs data when available.. Then you try and load this page asynchronously via Javascript, when it fails you retry, when it succeeds you do something with the data (display it, usually)
长轮询可以被描述为“HTTP推送”,基本上你有一个(Python)脚本通过web服务器提供,它只在可用时输出数据。然后你尝试通过Javascript异步加载这个页面,当它失败时你重试,当它成功时,你对数据做一些事情(通常显示它)
The examples in my answer are in PHP, but it it's only really 2 commands (sleep(rand(1, 10))
- the other few are to demonstrate the javascript's error handling)
我的答案中的例子是PHP,但它只有2个命令(睡眠(rand(1,10)) - 其他几个是演示javascript的错误处理)
Well, it's not quite that simple.. You can't just serve a CGI python script via Apache, because you will run out of worker-threads, and the web-server will not be able to accept any further connections.. So, you need to use a more specialised server..
好吧,它不是那么简单..你不能只通过Apache提供CGI python脚本,因为你将耗尽工作线程,并且web服务器将无法接受任何进一步的连接..所以,你需要使用更专业的服务器..
- The twisted Python framework is perfect for such servers - the following two servers are incidentally both written with it
- cometd - the "most famous" long-polling server thing, although I never had much luck with the Python implementation
- slosh - seems extremely simply to use.. Implemented in Python, although you can interact with it via HTTP requests
扭曲的Python框架非常适合这样的服务器 - 以下两个服务器都是顺便写的
cometd - “最着名的”长轮询服务器的事情,虽然我从来没有运气过Python
slosh - 似乎非常简单地使用..在Python中实现,尽管你可以通过HTTP请求与它进行交互
#9
Is this for a real webapp? Or is this a convenience thing for you to view output in the browser? If it's more so for convenience, you could consider using mod_python.
这是真正的webapp吗?或者这对您在浏览器中查看输出是否方便?如果方便起见,可以考虑使用mod_python。
mod_python is an extension for the apache webserver that embeds a python interpreter in the web server (so the script runs server side). It would easily let you do this sort of thing locally or for your own convenience. Then you could just run the script with mod python and have the handler post your results. You could probably easily implement the refreshing too, but I would not know off the top of my head how to do this.
mod_python是apache webserver的扩展,它在Web服务器中嵌入了python解释器(因此脚本运行服务器端)。它很容易让你在本地或为了方便自己做这件事。然后你可以用mod python运行脚本并让处理程序发布你的结果。您也可以轻松实现刷新,但我不知道如何做到这一点。
Hope this helps... check out mod_python. It's not too bad once you get everything configured.
希望这有帮助...查看mod_python。一旦配置好所有内容,它就不会太糟糕。
#1
This question appears to have two things in it.
这个问题似乎有两件事。
-
Presentation on the web. This is easy to do in Python -- use Django or TurboGears or any Python-based web framework.
在网上发表演讲。这在Python中很容易 - 使用Django或TurboGears或任何基于Python的Web框架。
-
Refresh of the web page to show new data. This can be done two ways.
刷新网页以显示新数据。这可以通过两种方式完成。
-
Some fancy Javascript to refresh.
一些花哨的Javascript刷新。
-
Some fancy HTML to refresh the page. The meta refresh tag is what you want. If you do this, you have an all-Python solution.
一些花哨的HTML来刷新页面。元刷新标记是您想要的。如果你这样做,你有一个全Python解决方案。
-
#2
If you want a dead simple way to print data from a Python script to a webpage and update automatically, you can just print from the script. For example, using Apache with the below Python CGI script:
如果您想要一种简单的方法将数据从Python脚本打印到网页并自动更新,您只需从脚本中打印即可。例如,将Apache与下面的Python CGI脚本一起使用:
#!/usr/bin/python
import time
import sys
import random
def write(inline=''):
sys.stdout.write(inline)
sys.stdout.write('\r\n')
sys.stdout.flush()
#prints out random digits between 1 and 1000 indefinitely
write("Content-type: text/html\r\n")
i = 0
while(True):
i = i + 1
time.sleep(1)
write(str(i) + "<br />")
If I navigate to that in a browser (Firefox, don't know if other browsers might work differently with regards to buffering etc), it prints the digits continually. Mind you, it prints in sequential order so the newer data is at the bottom rather than that top, but it might work depending on what exactly you're looking to do.
如果我在浏览器中导航到它(Firefox,不知道其他浏览器在缓冲方面是否可能有不同的工作方式),它会不断打印数字。请注意,它按顺序打印,因此较新的数据位于底部而不是顶部,但它可能会起作用取决于您想要做什么。
If this isn't really what you're looking for, the only other way to do this is an automatic refreshing page (either in an iframe, or the whole page) or with javascript to do the data fetching.
如果这不是您真正想要的,那么唯一的另一种方法是自动刷新页面(在iframe中,或在整个页面中)或使用javascript来进行数据获取。
You can use a meta refresh tag in your iframe or page HTML source, and your CGI can print the new data each time it's refreshed. Alternatively, you can use javascript with an XMLHTTPRequest to read the new data in without a visual page refresh.
您可以在iframe或页面HTML源中使用元刷新标记,并且每次刷新时,CGI都可以打印新数据。或者,您可以使用带有XMLHTTPRequest的javascript来读取新数据,而无需可视化页面刷新。
#3
You could use Comet, but I strongly discourage you from doing so. I'd just write a short Javascript, using jQuery this is really straightforward.
你可以使用Comet,但我强烈反对你这样做。我只是写一个简短的Javascript,使用jQuery这真的很简单。
Another possibility is the use of an iframe that reloads every 30 seconds, this would prevent the whole page from reloading.
另一种可能性是使用每30秒重新加载的iframe,这将阻止整个页面重新加载。
#4
If you want to do it entirely in python you can use pyjamas.
如果你想完全在python中完成它,你可以使用睡衣。
It generates javascript directly from python code, so you avoid writing javascript yourself completely.
它直接从python代码生成javascript,因此您可以避免完全自己编写javascript。
#5
JavaScript is the primary way to add this sort of interactivity to a website. You can make the back-end Python, but the client will have to use JavaScript AJAX calls to update the page. Python doesn't run in the browser, so you're out of luck if you want to use just Python.
JavaScript是向网站添加此类交互性的主要方式。您可以制作后端Python,但客户端必须使用JavaScript AJAX调用来更新页面。 Python不能在浏览器中运行,所以如果你只想使用Python,那就太不走运了。
(It's also possible to use Flash or Java applets, but that's a pretty heavyweight solution for what seems like a small problem.)
(它也可以使用Flash或Java小程序,但对于看似小问题的东西来说,这是一个非常重要的解决方案。)
#6
You need Javascript in one way or another for your 30 second refresh. Alternatively, you could set a meta tag refresh for every 30 seconds to redirect to the current page, but the Javascript route will prevent page flicker.
您需要以某种方式使用Javascript进行30秒刷新。或者,您可以设置元标记刷新每30秒重定向到当前页面,但Javascript路由将阻止页面闪烁。
#7
Write your output to a log file, and load the log file to the browser thru web server. If you need auto refresh, create a template HTML file with tag to refresh every 15 seconds:
将输出写入日志文件,并通过Web服务器将日志文件加载到浏览器。如果需要自动刷新,请创建一个带有标记的模板HTML文件,每15秒刷新一次:
<META HTTP-EQUIV="refresh" CONTENT="15">
and use server side include to include the log file on the page.
并使用服务器端include包括页面上的日志文件。
#8
Perhaps "long polling" is what you're looking for?
也许“长期民意调查”正是你要找的?
Long polling could be described as "HTTP push", basically you have a (Python) script served via a web-server, which only outputs data when available.. Then you try and load this page asynchronously via Javascript, when it fails you retry, when it succeeds you do something with the data (display it, usually)
长轮询可以被描述为“HTTP推送”,基本上你有一个(Python)脚本通过web服务器提供,它只在可用时输出数据。然后你尝试通过Javascript异步加载这个页面,当它失败时你重试,当它成功时,你对数据做一些事情(通常显示它)
The examples in my answer are in PHP, but it it's only really 2 commands (sleep(rand(1, 10))
- the other few are to demonstrate the javascript's error handling)
我的答案中的例子是PHP,但它只有2个命令(睡眠(rand(1,10)) - 其他几个是演示javascript的错误处理)
Well, it's not quite that simple.. You can't just serve a CGI python script via Apache, because you will run out of worker-threads, and the web-server will not be able to accept any further connections.. So, you need to use a more specialised server..
好吧,它不是那么简单..你不能只通过Apache提供CGI python脚本,因为你将耗尽工作线程,并且web服务器将无法接受任何进一步的连接..所以,你需要使用更专业的服务器..
- The twisted Python framework is perfect for such servers - the following two servers are incidentally both written with it
- cometd - the "most famous" long-polling server thing, although I never had much luck with the Python implementation
- slosh - seems extremely simply to use.. Implemented in Python, although you can interact with it via HTTP requests
扭曲的Python框架非常适合这样的服务器 - 以下两个服务器都是顺便写的
cometd - “最着名的”长轮询服务器的事情,虽然我从来没有运气过Python
slosh - 似乎非常简单地使用..在Python中实现,尽管你可以通过HTTP请求与它进行交互
#9
Is this for a real webapp? Or is this a convenience thing for you to view output in the browser? If it's more so for convenience, you could consider using mod_python.
这是真正的webapp吗?或者这对您在浏览器中查看输出是否方便?如果方便起见,可以考虑使用mod_python。
mod_python is an extension for the apache webserver that embeds a python interpreter in the web server (so the script runs server side). It would easily let you do this sort of thing locally or for your own convenience. Then you could just run the script with mod python and have the handler post your results. You could probably easily implement the refreshing too, but I would not know off the top of my head how to do this.
mod_python是apache webserver的扩展,它在Web服务器中嵌入了python解释器(因此脚本运行服务器端)。它很容易让你在本地或为了方便自己做这件事。然后你可以用mod python运行脚本并让处理程序发布你的结果。您也可以轻松实现刷新,但我不知道如何做到这一点。
Hope this helps... check out mod_python. It's not too bad once you get everything configured.
希望这有帮助...查看mod_python。一旦配置好所有内容,它就不会太糟糕。