Skip to main content

A simple yet powerful websocket cli

Project description

websockets-cli

Pypi version Coverage Status Documentation Status Code Style License Apache 2

A simple yet powerful websocket cli.

Why?

Each time I work on a web project involving websockets, I found myself wanting a simple (cli) tool to test what I have coded. What I often do is to write a python script using websockets. There are graphical tools like Postman, but I'm not confortable with. So I decided to write a cli tool for this purpose.

Installation

You can install the cli with pip:

$ pip install websockets-cli

or use a better package manager like poetry:

# you probably want to add this dependency as a dev one, this is why I put -D into square brackets
$ poetry add [-D] websockets-cli

ws starts working from python3.7 and also supports pypy3. It has the following dependencies:

Usage

The usage is straightforward and the cli is well documented.

$ ws
Usage: ws [OPTIONS] COMMAND [ARGS]...

  A convenient websocket cli.

  Example usage:

  # listens incoming messages from endpoint ws://localhost:8000/path
  $ ws listen ws://localhost:8000/path

  # sends text "hello world" in a text frame
  $ ws text wss://ws.postman-echo.com/raw "hello world"

  # sends the content from json file "hello.json" in a binary frame
  $ ws byte wss://ws.postman-echo.com/raw file@hello.json

Options:
  --version   Show the version and exit.
  -h, --help  Show this message and exit.

Commands:
  byte                Sends binary message to URL endpoint.
  echo-server         Runs an echo websocket server.
  install-completion  Install completion script for bash, zsh and fish...
  listen              Listens messages on a given URL.
  ping                Pings a websocket server located at URL.
  pong                Sends a pong to websocket server located at URL.
  session             Opens an interactive session to communicate with...
  tail                An emulator of the tail unix command that output...
  text                Sends text message on URL endpoint.

The first command to use is install-completion to have auto-completion for commands and options using the TAB key. Auto-completion is available on bash, fish and zsh. For Windows users, I don't forget you (I'm also a Windows user), support is planned for Powershell ;)

$ ws install-completion
# when the command succeeded, you should see the following message
Successfully installed completion script!

To play with the api you can use the websocket server kindly provided by the Postman team at wss://ws.postman-echo.com/raw or spawn a new one with the following command:

# it will listen incoming messages on port 8000, to stop it, just type Ctrl+C
$ ws echo-server -p 8000
Running server on localhost:8000 💫

To ping the server, you can do this:

# :8000 is a
$ ws ping :8000
PING ws://localhost:8000 with 32 bytes of data
sequence=1, time=0.00s

To send a message, you can type this:

# Sends a text frame
$ ws text :8000 "hello world"  # on Windows it is probably better to use single quotes 'hello world'
Sent 11.0 B of data over the wire.

# Sends a binary frame
$ ws byte :8000 "hello world"
Sent 11.0 B of data over the wire.

If you know that you will have a long interaction with the server, it is probably better to use the session subcommand.

$ ws session wss://ws.postman-echo.com/raw
Welcome to the interactive websocket session! 🌟
For more information about commands, type the help command.
When you see <> around a word, it means this argument is optional.
To know more about a particular command type help <command>.
To close the session, you can type Ctrl+D or the quit command.

> ping "with payload"
PING wss://ws.postman-echo.com/raw with 12 bytes of data
Took 0.16s to receive a PONG.

> quit
Bye! 👋

Documentation

The full documentation can be found at https://pyws.readthedocs.io

Limitations

The cli does not support RFC 7692 and RFC 8441 because trio_websocket the underlying library used for websockets does not support it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

websockets-cli-0.2.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

websockets_cli-0.2.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file websockets-cli-0.2.0.tar.gz.

File metadata

  • Download URL: websockets-cli-0.2.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.7.13 Linux/5.13.0-1031-azure

File hashes

Hashes for websockets-cli-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f07ed7fd87f548e78e83a0effae66bfec47ead06de91343ac358e89d0a8259bd
MD5 d92b8c5e8b0193ee2220898a68aa4114
BLAKE2b-256 fb5c7e19c6570ade89adc0a10dea069bbbdf6b68412dab89ee4322cd870bdd0b

See more details on using hashes here.

File details

Details for the file websockets_cli-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: websockets_cli-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.7.13 Linux/5.13.0-1031-azure

File hashes

Hashes for websockets_cli-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cde28484b8e29dbc127c33f5d89cfb69a0e5185e7154d36c7d32cd499fb56c20
MD5 4442a7b5c7515d39daca8dc51ece57e8
BLAKE2b-256 2f43ce15569bf971b20e3dd286fb5c019639a6e328019faf751b30557744c50b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page