惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security @ Cisco Blogs
雷峰网
雷峰网
T
Threat Research - Cisco Blogs
D
Docker
The Hacker News
The Hacker News
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Project Zero
Project Zero
C
Cyber Attacks, Cyber Crime and Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
AWS News Blog
AWS News Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
Martin Fowler
Martin Fowler
T
Threatpost
S
Schneier on Security
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
AI
AI
T
Tenable Blog
K
Kaspersky official blog
博客园 - 叶小钗
V
V2EX
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
Arctic Wolf
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
T
Tor Project blog
J
Java Code Geeks
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
B
Blog
P
Proofpoint News Feed
爱范儿
爱范儿
WordPress大学
WordPress大学
I
InfoQ
小众软件
小众软件
月光博客
月光博客
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
Help Net Security
Help Net Security

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
websocketd:一个小而美的管道工具
2017-03-31 · via 夜行人

文章目录

do one thing and do it well

以*nix哲学来看,websocketd是个优雅的工具,小而美,作为管道,有强大的可组合性

简介

Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.

websocketd是个命令行工具,他能轻易地包装其他命令行,使其能够通过websocket来访问

以上就是它做的所有事情了,简单的几乎不需要学习成本,就像ls/cat/netcat这些你熟悉的工具一样

上手

###安装 如果你是mac用户,直接使用brew安装就行:brew install websocketd,如果你是其他操作系统用户,点击Download and install

###使用 官方给出了一个非常简单的例子

创建一个count.sh文件,内容为:

1
2
3
4
5
#!/bin/bash
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
    echo $COUNT
    sleep 1
done

使其可执行:chmod +x count.sh

我们先在本地运行试试:./count.sh,输出结果为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
1
2
3
4
5
6
7
8
9
10

接下来我们将其转为一个websocket服务: websocketd --port=8080 ./count.sh

现在我们就可以在网页上看结果了

count.html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<head>
<title>websocketd test</title>
</head>
<body>
<pre id="log"></pre>
<script>
  // helper function: log message to screen
  function log(msg) {
        document.getElementById('log').textContent += msg + '\n';
          }
  // setup websocket with callbacks
  var ws = new WebSocket('ws://localhost:8080/');
    ws.onopen = function() {
          log('CONNECT');
            };
  ws.onclose = function() {
        log('DISCONNECT');
          };
  ws.onmessage = function(event) {
        log('MESSAGE: ' + event.data);
          };
</script>
</body>

直接用浏览器打开这个本地页面就行!(wesocket可以跨域)

非常简单!

与特定编程语言结合

websocketd可以与各种语言编写的程序结合,官方源码库的example里就给出了很多:

  • bash
  • c#
  • cgi-bin
  • f#
  • haskell
  • html
  • java
  • nodejs
  • perl
  • php
  • python
  • ruby
  • swift
  • windows-jscript
  • windows-vbscript

总有一款适合你

如果你的语言笔记小众,只要你的程序和shell脚本的输入/输出模式一样,就没问题

与python结合

我日常主要用python,所以列出与python的整合例子

同样来自官方的例子:count.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/usr/bin/env python
  # encoding: utf-8
  import time
  from sys import stdout
  i=1
  while True:
      time.sleep(1)
      i=i+1
      print(i)
      stdout.flush()

使其可执行:chmod +x count.py

运行:websocketd --port=8080 ./count.py

你讲看到和上边一样的结果

上边的源码有一处值得留意:stdout.flush(),有兴趣的同学可以参考Usage of sys.stdout.flush() method

一个可交互的例子

前头的例子都是单向的数据流,我们来看看一个交互性更强的例子(greeter.py)

1
2
3
4
5
6
7
from sys import stdin, stdout

# For each line FOO received on STDIN, respond with "Hello FOO!".
while True:
  line = stdin.readline().strip()
  print('Hello %s!' % line)
  stdout.flush() # Remember to flush

运行它:

1
2
chmod +x count.py
websocketd --port=8080 ./greeter.py

对应的前端为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<head>
<title>websocketd test</title>
</head>
<body>
<pre id="log"></pre>
<script>
  // helper function: log message to screen
  function log(msg) {
        document.getElementById('log').textContent += msg + '\n';
          }
  // setup websocket with callbacks
  var ws = new WebSocket('ws://localhost:8080/');
  ws.onopen = function() {
          log('CONNECT');
          // input
          ws.send("wwj");
            };
  ws.onclose = function() {
        log('DISCONNECT');
          };
  ws.onmessage = function(event) {
        log('MESSAGE: ' + event.data);
          };
</script>
</body>

打开页面,我们看到:

Awesome!

交互调试

调试带有交互功能的程序,手写js比较不方便,官方也给出了调试模式

以上边的例子为例:websocketd --port=8000 --dir=./ --devconsole

打开:http://localhost:8000/greeter.py

一些特性

关于websocketd的特性介绍你可以看它的主页

我这里对我发现的特性做个笔记:

  • 支持多client连接,每个client连接后,都将看到count.sh从头开始运行的过程数据(应该是独立的,每来一个请求,被包装的脚本从头运行?)

与其他工具组合

同*nix的其他管道工具一样,组合的可能性取决于你的想象力,在此列出我觉得有用的组合:

  • ngrok组合,将websocket服务暴露到公网(当然你也可以用nginx反向代理)
  • ws-repl,这个项目将websocketd与各种REPL对接(ps,这类需求我更推荐gotty)

参考

文章作者 种瓜

上次更新 2017-03-31