Package: websockets-cli
websockets-cliA 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 usingwebsockets.
There are graphical tools likePostman, but I'm not confortable with.
So I decided to write a cli tool for this purpose.InstallationYou can install the cli withpip:$pipinstallwebsockets-clior use a better package manager likepoetry:# you probably want to add this dependency as a dev one, this is why I put -D into square brackets$poetryaddwebsockets-cli-Gdevws starts working frompython3.8and also supportspypy3. It has the following dependencies:triofor structured (async) concurrency support.trio-websocketthe library implementing the websocket protocol.pydantic/pydantic-settingsfor
input validation and settings management.certifito manage TLS and certificates.clickto write the cli.click-didyoumeanfor command suggestions in case of typos.richfor beautiful output display.shellinghamto detect the shell used.UsageThe usage is straightforward and the cli is well documented.$ws
Usage:ws[OPTIONS]COMMAND[ARGS]...Aconvenientwebsocketcli.Exampleusage:# listens incoming messages from endpoint ws://localhost:8000/path$wslistenws://localhost:8000/path# sends text "hello world" in a text frame$wstextwss://ws.postman-echo.com/raw"hello world"# sends the content from json file "hello.json" in a binary frame$wsbytewss://ws.postman-echo.com/rawfile@hello.json

Options:--versionShowtheversionandexit.-h,--helpShowthismessageandexit.

Commands:byteSendsbinarymessagetoURLendpoint.echo-serverRunsanechowebsocketserver.install-completionInstallcompletionscriptforbash,zshandfish...listenListensmessagesonagivenURL.pingPingsawebsocketserverlocatedatURL.pongSendsapongtowebsocketserverlocatedatURL.sessionOpensaninteractivesessiontocommunicatewith...tailAnemulatorofthetailunixcommandthatoutput...textSendstextmessageonURLendpoint.The first command to use isinstall-completionto have auto-completion for commands and options using theTABkey.
Auto-completion is available onbash,fishandzsh. For Windows users, I don't forget you (I'm also a Windows
user), support is planned forPowershell;)$wsinstall-completion# when the command succeeded, you should see the following messageSuccessfullyinstalledcompletionscript!To play with the api you can use the websocket server kindly provided by thePostmanteam 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$wsecho-server-p8000Runningserveronlocalhost:8000💫Topingthe server, you can do this:# :8000 is a$wsping:8000
PINGws://localhost:8000with32bytesofdatasequence=1,time=0.00sTo send a message, you can type this:# Sends a text frame$wstext:8000"hello world"# on Windows it is probably better to use single quotes 'hello world'Sent11.0Bofdataoverthewire.# Sends a binary frame$wsbyte:8000"hello world"Sent11.0Bofdataoverthewire.If you know that you will have a long interaction with the server, it is probably better to use thesessionsubcommand.$wssessionwss://ws.postman-echo.com/raw
Welcometotheinteractivewebsocketsession!🌟
Formoreinformationaboutcommands,typethehelpcommand.
Whenyousee<>aroundaword,itmeansthisargumentisoptional.
Toknowmoreaboutaparticularcommandtypehelp<command>.
Toclosethesession,youcantypeCtrl+Dorthequitcommand.

>ping"with payload"PINGwss://ws.postman-echo.com/rawwith12bytesofdata
Took0.16storeceiveaPONG.

>quit
Bye!👋DocumentationThe full documentation can be found athttps://pyws.readthedocs.ioLimitationsThe cli does not supportRFC 7692andRFC 8441becausetrio_websocketthe underlying library used for
websockets does not support it.