用python控制web

purl的Python项目详细描述


Binder

Purly

用python控制网络:snake:

安装

要安装稳定版本:

pip install purly

安装开发版本:

be sure to install npm first!

# clone the repository
git clone https://github.com/rmorshea/purly
cd purly && bash scripts/build.sh && bash scripts/install.sh

入门

运行以下代码片段,然后导航到http://127.0.0.1:8000/model/index

importpurly# Prepare your layoutpurly.state.Machine().run(debug=False)layout=purly.Layout('ws://127.0.0.1:8000/model/stream')# create your HTMLdiv=layout.html('div')div.style.update(height='20px',width='20px',background_color='coral')# add it to the layoutlayout.children.append(div)# and sync it!layout.sync()

现在你的作品应该神奇地出现在你打开的浏览器页面上了!

div with some styling

架构(非最终版)

purly的基本目标是让python尽可能多地控制一个网页,并在一个非常简单的包中实现。有一个主要问题阻碍了这个目标的实现——数据同步。对于这个问题,purly的答案是它的model server,对于任何连接到它并遵守它的protocol的客户机来说,它充当了state of a webpage的“真理之源”。

型号服务器

protocol

purly使用web套接字服务器来保持多个并发客户端同步。上面的动画显示了两个客户机(一个python客户机将更新推送到一个浏览器),但是您可以让更多的客户机生成和/或使用模型更新。每个客户机都与一个模型(任何json可序列化对象)关联,但是服务器上可以存储多个模型。客户端通过在套接字路由中指定特定模型的名称(例如ws://host:port/model/<model-name>/stream)来连接到该模型。只有连接到同一模型的客户端才能通过服务器相互通信。

型号规格

虽然模型服务器支持任何json可序列化模型,但purly作为控制web的框架必须:

  1. 尽可能充分地交流dom元素的结构及其各种交互。
  2. 通过网络向dom模型发送更新,简单易懂的包:
  • 为了减少网络流量,更新消息的大小必须很小。

为了实现上述目标,我们提出了一个平面dom模型:

Model={id:Element,# Maps a uniquely identifiable string to an Element.root:Element,# The id "root" should always indicate the outermost Element....}
Element={tagName:string# Standard HTML tags like h1, table, div, etc.signature:string# The hash of this element attributes, and the hashes of its children.children:[string,# Any arbitrary string.{type:'ref','ref':string},# An object where the key "ref" refers to the "key" attribute....],attributes:{key:id,# The id that uniquely identifies this Element.parent_key:id# The unique id of this element's parent.attr:value,# Map any attribute name to any JSON serializable value.on<Event>:{# Specify an event callback with an attribute of the form "on<Event>".callback:uuid,# A unique identifier by which to refer to the callback function.keys:[...],# Details of the event to pass on to the callback.update:[...]# Any attributes that should be synced before the callback is triggered.}}}

Purly模型示例

下面的html

<divkey='root'>
  Make a selection:
  <inputtype='text'key='abc123'></input><div>

将与以下Purly型号进行通信:

{root:{tag:'div',elements:['Make a selection:'{'ref':'abc123'},]attributes:{'key':'root'}},abc123:{tag:'input',elements:[],attributes:{'key':'abc123','type':'text',},}}

通信协议

purly模型服务器发送和接收json可序列化数组,这些数组包含Message形式的对象。

[Message,# a dict conforming to the Message spec...]

消息

有两种类型的消息-更新和信号-但是都符合以下格式。

Message={"header":{"type":"signal"or"update",# Message type (indicates the kind of content)."version":"0.1",# Message protocol version.}# Content which depends on the message type."content":dict,}

信号

  • 信号不会修改模型服务器的状态。
  • 信号content未经修改就分发给其他模型客户机。

更新

  • content字段指定将合并到模型中的更改。
  • 只有更新content和模型之间的差异才会分发给其他客户机。
  • 更新合并以嵌套方式执行

如果模型的当前状态是

{'a':{'b':1,'c':1,}}

以及更新消息

{'a':{'c':2}}

收到时,生成的模型状态为

{'a':{'b':1,'c':2,}}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java OnClick用于ListView中的特定项   java如何多次循环相同的TestNG测试。包括课前和课后   java如何在Codenameone中设置BrowserComponent浏览器窗口大小   java Socketchannel始终为空   java以编程方式向JavaFX WebEngine历史添加新条目   java Hibernate+Spring与Oracle一起使用分层查询(从+连接方式开始)   Java中的安卓 getView和Beacon(Estimote)问题   计时器如何在java中设置暂停选项   java为什么输入1000000000000的输出不正确?   java如何在程序执行期间更改log4j2中的日志记录级别   java阻止代码引发XML异常   未找到媒体类型为application/json的java JAX RS MessageBodyWriter   java如何将BuffereImage中的特定颜色(0xFF00FF)设置为透明?   java如何列出web元素,如果网页中没有特定链接的id,如何单击网页中的特定链接?   在Java中读取txt文件时获取EOFEException   java如何避免为每个视图添加inject方法?   JavaSpringController:Facebook上类似文章的永久链接   java在尝试执行脚本时遇到错误“net.serentiybdd.core.exceptions.serentitymanagedexception:No session ID”