Metadata-Version: 2.1
Name: httpie-websockets
Version: 0.5.4
Summary: Websocket plugin for httpie
Keywords: http,httpie,plugin,websocket,websockets
Author-Email: belingud <1170202353@qq.com>
License: MIT License
        
        Copyright (c) 2024 belingud
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Environment :: Console
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development
Classifier: Topic :: System :: Networking
Classifier: Topic :: Terminals
Classifier: Topic :: Text Processing
Classifier: Topic :: Utilities
Project-URL: Homepage, https://github.com/belingud/httpie-websockets
Project-URL: Documentation, https://github.com/belingud/httpie-websockets
Project-URL: Issues, https://github.com/belingud/httpie-websockets/issues
Project-URL: Source, https://github.com/belingud/httpie-websockets
Project-URL: Github, https://github.com/belingud/httpie-websockets
Requires-Python: >=3.8
Requires-Dist: requests>=2.31.0
Requires-Dist: httpie>=3.2.3
Requires-Dist: websocket-client>=1.8.0
Requires-Dist: python-socks>=2.5.0
Description-Content-Type: text/markdown

# httpie-websockets

[![PyPI version](https://img.shields.io/pypi/v/httpie-websockets?style=for-the-badge)](https://pypi.org/project/httpie-websockets/) [![License](https://img.shields.io/github/license/belingud/httpie-websockets.svg?style=for-the-badge)](https://opensource.org/licenses/MIT) ![Static Badge](https://img.shields.io/badge/language-Python-%233572A5?style=for-the-badge) ![PyPI - Downloads](https://img.shields.io/pypi/dm/httpie-websockets?logo=python&style=for-the-badge) ![Pepy Total Downlods](https://img.shields.io/pepy/dt/httpie-websockets?style=for-the-badge&logo=python)

Home: https://github.com/belingud/httpie-websockets


<!-- TOC -->
* [httpie-websockets](#httpie-websockets)
  * [Features](#features)
  * [Install](#install)
  * [Usage](#usage)
    * [Debug Log](#debug-log)
    * [Proxy & Cert](#proxy--cert)
    * [Headers](#headers)
    * [Subprotocol](#subprotocol)
    * [Auth](#auth)
    * [Session](#session)
    * [Verify](#verify)
    * [Timeout](#timeout)
    * [Messages Download](#messages-download)
    * [Multi-line Input Support](#multi-line-input-support)
  * [Uninstall](#uninstall)
<!-- TOC -->

`httpie-websockets` is an HTTPie CLI plugin that adds WebSocket support to the HTTPie command line.

## Features

- **WebSocket Support:** Seamlessly connect to WebSocket servers using the familiar HTTPie command line interface.
- **Bidirectional Communication:** Send and receive messages in real-time.
- **Secure Connections:** Supports both `ws://` and `wss://` protocols.
- **Easy Integration:** Simple installation and usage within the HTTPie environment.

## Install

You can install by httpie plugins command:

```shell
httpie plugins install httpie-websockets
```

or use pip in the same environment with httpie

```shell
pip install httpie-websockets
```

If your `httpie` is installed with `pipx`, you also can use `pipx` to install `httpie-websockets`, If you cannot use it
properly after installation。

Suppose your httpie environment is named httpie.

```shell
# Replace httpie with your httpie venv name
pipx inject httpie httpie-websockets  # will auto upgrade version
# or
pipx runpip httpie install -U httpie-websockets
```

## Usage

After install this plugin, just pass websocket url to `http` command.

```shell
http ws://localhost:8000/ws
```

This allows HTTPie to interact with WebSocket servers directly from the command line.

Example:

```shell
$ http wss://echo.websocket.org
> wss://echo.websocket.org
Type a message and press enter to send it
Press Ctrl+C to close the connection

```

When you press CTRL+C, connection will disconnect and httpie will get handshake response headers
and websocket connection info with close code and close message like below:

```shell
^C
Oops! Disconnecting. Need to force quit? Press again!
HTTP/1.1 200
connection: Upgrade
date: Thu, 15 Aug 2024 13:24:10 GMT
fly-request-id: 01J5B3BHGV549MMJQ474SF7J60-sin
sec-websocket-accept: MV41qn7qZQP3IXsTzYS5eDRe2tE=
server: Fly/ddfe15ec (2024-08-14)
upgrade: websocket
via: 1.1 fly.io

Websocket connection info:
Close Code: 1006
Close Msg: KeyboardInterrupt
```

### Debug Log

You can set `HTTPIE_WS_LOG_LEVEL` to `DEBUG` to see `httpie_websocket` debug log for more information.

On linux and Mac:

```shell
export HTTPIE_WS_LOG_LEVEL=DEBUG
```
Or

```shell
HTTPIE_WS_LOG_LEVEL=DEBUG http wss://echo.websocket.org
```

On Windows:

```shell
set HTTPIE_WS_LOG_LEVEL=DEBUG
```

Or

```shell
# Powershell
$env:HTTPIE_WS_LOG_LEVEL="DEBUG"; http wss://echo.websocket.org
# Cmd
cmd /C "set HTTPIE_WS_LOG_LEVEL=DEBUG &&; http wss://echo.websocket.org"
```

### Proxy & Cert

This project using `websocket-client` to establish connection, support proxy and custom cert file.
You can pass proxy and cert to httpie.

Support `http`, `socks4`, `socks4a`, `socks5` and `socks5h` proxy protocol.

```shell
http wss://echo.websocket.org --proxy=http://proxy.com
http wss://echo.websocket.org --proxy=socks4://proxy.com
http wss://echo.websocket.org --proxy=socks4a://proxy.com
http wss://echo.websocket.org --proxy=socks5://proxy.com
http wss://echo.websocket.org --proxy=socks5h://proxy.com
```

Custom cert file same as httpie.

```shell
http wss://yourservice.com --cert=/path/to/cert --cert-key=/path/to/cert-key --cert-key-pass=pass
```

### Headers

Also support custom headers, you can send header through httpie.

**Note** `wss://echo.websocket.org` does not support any authentication, and will ignore any headers you send.


```shell
http wss://echo.websocket.org Custom-Header:Custom-value
```

### Subprotocol

You can send subprotocols from the headers.
Multiple subprotocols need to be separated by commas, since httpie receives only the first one with the same headers key.

```shell
http wss://echo.websocket.org Sec-WebSocket-Protocol:sub1,sub2
```

### Auth

Support pass auth option and auth-type.

**basic**: httpie use basic auth as default.

```shell
http wss://echo.websocket.org --auth=user1:pass
```

Websocket server will receive a header like `'Authorization': 'Basic dXNlcjE6dGVzdA=='`.

**bearer**: similar with basic.

```shell
http wss://echo.websocket.org --auth-type=bearer --auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
```

Websocket server will receive a header like `'Authorization': 'Bearer eyxxxx'`

**digest**: Technically, digest authentication is not supported, but you can generate an auth
header manually if you want.

```shell
http wss://echo.websocket.org "Authorization: Digest username='user', realm='example', nonce='c3a7f38c-5e5a-45b2-a5b5-3b5e2c5c5c5c', uri='/path/to/protected/resource', response='generated_response', qop=auth, nc=00000001, cnonce='generated_cnonce', opaque='6d6b8f8f-6b8f-6b8f-6b8f-6b8f6b8f6b8f'"
```

### Session

Support session option and perform as a header.

```shell
http wss://echo.websocket.org -s user1
```

Similar like basic auth, server will receive a header like `'Authorization': 'Basic dXNlcjE6dGVzdA=='`.

### Verify

You can disable SSL verification by using the --verify=no option

```shell
http wss://echo.websocket.org --verify=no
```

### Timeout

Pass time out option to waiting for connection establish.

```shell
http wss://echo.websocket.org --timeout=3
```

### Messages Download

Support download messages in bytes for httpie --download option. Including send and receive messages.

```shell
http wss://echo.websocket.org --download -o msgs.txt
< Request served by 1781505b56ee58
> Connected to wss://echo.websocket.org
Type a message and press enter to send it
Press Ctrl+C to close the connection
> hello              <-- sent message
< hello              <-- received message
>                    <-- waiting for input
```
When you press Ctrl+C, you will see:

```shell
> ^C
Oops! Disconnecting. Need to force quit? Press again!
HTTP/1.1 200
connection: Upgrade
date: Wed, 21 Aug 2024 09:19:00 GMT
fly-request-id: 01J5T3PY9EYSR9R7H6X810X4XK-nrt
sec-websocket-accept: cUDDauCuW1/u9RS5Nbcw7bYUl/8=
server: Fly/a7508dd9 (2024-08-20)
upgrade: websocket
via: 1.1 fly.io

Downloading to msgs.txt
Done. 35 bytes in 00:0.09973 (350.95880538904555 bytes/s)
```

And in the `msgs.txt` file, you can see all sent and received messages include the `>` and `<` tags.

```text
< Request served by 1781505b56ee58    <-- echo.websocket.org connection msg
> test                                <-- sent msg
< test                                <-- received msg
```

If connection was closed by server, httpie will also download the messages.

### Multi-line Input Support

Coming soon.

## Uninstall

If you want to uninstall this plugin, use the same way when you install.

Installed by `httpie` command, uninstall by

```shell
httpie plugins uninstall httpie-websockets
```

Installed by `pip` command, uninstall by

```shell
pip uninstall httpie-websockets
```
