with
Server-Sent Events

                  @cppgohan
Flask?
Flask?
MicroFramework again...
Flask?
MicroFramework again...
Written in Python
=
=
=


Werkzeug
WSGI utility library
=

                       +
Werkzeug
WSGI utility library
=

                       +
Werkzeug
WSGI utility library
=

                       +
Werkzeug                   Jinja2
WSGI utility library       template engine
Demo
Hello.py
Demo
Hello.py
Demo
Hello.py
Demo
index.html
Demo
index.html




result???
Demo
index.html




result???

  • terminal run: python hello.py
  • open http://localhost:5000
Server-Sent Event
server push的html5标准, 用法简易, 单次连接比polling高效些.
相比websocket的双向二进制数据, Server-Sent Event只是单向, HTTP协议.
(但浏览器调试工具往往没法看到服务器发来的数据, X_X)




                                       Server对Client的请求,
                                       做stream方式的响应.
Server-Sent Event
Server-Sent Event


             支
                 持
                 率
                   不
              要  能 高, 暂
                兼 ”玩” 时
                 容      只
                   处
                     理
Demo

index.html
Demo
hello.py
Demo
redis:
Demo
redis:




   demo:FILE_TEXT -- 每次python读出来的文本
   demo:FILE_TEXT_SEQ -- 为文本版本号, 每次文本更新, 版本号+1
   /sever_push响应 -- 当FILE_TEXT_SEQ增加时, 将最新的文本返回给浏览器
Demo
result???
Demo
result???

  • terminal run: python hello.py
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
  • block!!!
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
  • block!!!
solution???
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
  • block!!!
solution???

  • async WSGI server! (eg. gunicorn + gevent, gevent)
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
  • block!!!
solution???

  • async WSGI server! (eg.-kgunicorn-bgevent, gevent)hello:app
                                     +
  • terminal run: gunicorn gevent '0.0.0.0:5000'
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
  • block!!!
solution???

  • async WSGI server! (eg.-kgunicorn-bgevent, gevent)hello:app
                                     +
  • terminal run: gunicorn gevent '0.0.0.0:5000'
  • open http://localhost:5000
Demo
result???

  • terminal run: python hello.py
  • open http://localhost:5000
  • block!!!
solution???

  • async WSGI server! (eg.-kgunicorn-bgevent, gevent)hello:app
                                     +
  • terminal run: gunicorn gevent '0.0.0.0:5000'
  • open http://localhost:5000 again!
  • open http://localhost:5000
More Demo

Server下发代码示例
More Demo

Server下发代码示例
• start http://localhost:5000
        web server
• open push js script...
  • server excute js script...
  • browser
  • ...
More Usage

• 网站动态更新
• 区分客户端推送
• 微博墙实现
• webgame服务器推送
• 浏览器插件
Resources

• http://www.python.org/dev/peps/pep-0333/
• http://werkzeug.pocoo.org/
• http://jinja.pocoo.org/
• http://flask.pocoo.org/
• http://www.gevent.org/
• http://gunicorn.org/
• http://dev.w3.org/html5/eventsource/
• http://www.html5rocks.com/en/tutorials/eventsource/basics/
• http://www.w3schools.com/html/html5_serversentevents.asp
END; 2013, 分享不止!

Flask With Server-Sent Event