【100分】简单得很!高分求python 或者webpy中如下代码怎么写?

ORACLE800 2013-07-25 08:05:48
高分求python 或者webpy中如下代码怎么写?谢谢。

下面是php的代码
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
...全文
198 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ORACLE800 2013-07-28
  • 打赏
  • 举报
回复
有人知道吗 谢谢
SFinx 2013-07-26
  • 打赏
  • 举报
回复
如果你非强调连日期输出风格都必须要保持一致的话,使用email.Utils里的formatdate()替换掉datetime.datetime.now()即可。
import web
from email.Utils import formatdate

urls = (
    '/','index'
    )

class index:
    def GET(self):
        return "data: The server time is:"+formatdate(localtime=True)
        
if __name__ == "__main__":
    app = web.application(urls,globals())
    app.run()
SFinx 2013-07-26
  • 打赏
  • 举报
回复
import web
import datetime

urls = (
    '/','index'
    )

class index:
    def GET(self):
        return "data: The server time is:"+str(datetime.datetime.now())
        
if __name__ == "__main__":
    app = web.application(urls,globals())
    app.run()
ORACLE800 2013-07-26
  • 打赏
  • 举报
回复
补充详细我的意思是 ,我想知道怎么从服务端发送到客户端,刚的代码来自下面这里的。很纠结没有python代码实例 谢谢啦。 http://www.w3school.com.cn/html5/html_5_serversentevents.asp
ORACLE800 2013-07-26
  • 打赏
  • 举报
回复
谢谢这位大侠 不过大侠木有理解我的意思。我的意思是下面的几行如何用web.py或者python的语法来写? header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); $time = date('r'); echo "data: The server time is: {$time}\n\n"; flush();
ORACLE800 2013-07-26
  • 打赏
  • 举报
回复
非常感谢SFinx兄。 我写了客户端代码和服务端代码,可以运行了 但是发现很郁闷,客户端获取的更新好像不是实时的?每次差了5秒,我应该怎样才能获得实时的更新呢?
<!DOCTYPE html>
<html>
<body>
<h1>获得服务器更新</h1>
<div id="result"></div>

<script>
if(typeof(EventSource)!=="undefined")
  {
  var source=new EventSource("/update");
  source.onmessage=function(event)
    {
    document.getElementById("result").innerHTML+=event.data + "<br />";
    };
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
  }
</script>

</body>
</html>
服务端代码:



class Update:
        def GET(self):
                web.header('Content-type','text/event-stream')
                web.header('Cache-Control','no-cache')
                return 'data:'+datetime.datetime.now().strftime('%Y%m%d%H%M%S')


输出结果: 获得服务器更新 20130726161615 20130726161620 20130726161625 20130726161630 20130726161635 20130726161640 20130726161645 20130726161650 20130726161655 20130726161700 20130726161705 20130726161710 20130726161715 20130726161720 20130726161725 20130726161730 20130726161735 20130726161740 20130726161745 20130726161750 20130726161755 20130726161800 20130726161805 20130726161810
SFinx 2013-07-26
  • 打赏
  • 举报
回复
引用 6 楼 zhuche110 的回复:
非常感谢哈 那flush(); 怎么写呢?刚刚接触python和webpy不久 菜鸟哈 ^_^ 谢谢了!
flush()的作用是刷新服务器缓存,甚至被认为是特指apache的缓冲区。在这个场景中,python没有对应的flush()方法(文件操作有flush()),服务器缓存是由webserver自行处理的,也就是说不需要的。
ORACLE800 2013-07-26
  • 打赏
  • 举报
回复
非常感谢哈 那flush(); 怎么写呢?刚刚接触python和webpy不久 菜鸟哈 ^_^ 谢谢了!
SFinx 2013-07-26
  • 打赏
  • 举报
回复
的确理解错了 Server-Sent事件使用web.header()即可,具体可看web.py的手册。
class index:
    def GET(self):
        web.header('Content-Type','text/event-stream')
        web.header('Cache-Control', 'no-cache')
        return "data: The server time is:"+formatdate(localtime=True)
不过始终记得那句话“所有主流浏览器均支持服务器发送事件,除了 Internet Explorer。

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧