API Reference

A comprehensive documentation of anysocks’ public API.

Meta information

anysocks has some variables containing meta information about the project itself.

anysocks.__author__

The author of the library.

A string holding copyright information.

anysocks.__docformat__

The format of the documentation style.

anysocks.__license__

The license of this project.

anysocks.__title__

The actual project name.

anysocks.__version__

The version of the library, e.g. 0.1.2-alpha0.

Client

The heart of this library. It provides convenience functions to quickly establish a new WebSocket client connection without having to worry about deep logic.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import anyio
from anysocks import open_connection


async def main():
    async with open_connection('ws://echo.websocket.org') as con:
        print('Connection established!')

        # First, let's send some text to the server.
        text = input('What to send? ')
        await con.send(text)

        # Now, we receive and verify the server's response.
        message = await con.get_message()
        assert message == text, "Received {}, expected {}".format(message, text)

    print('Connection closed with code {}'.format(con.close_code.value))

anyio.run(main)

open_connection

create_websocket

WebSocket

anysocks.websocket wraps around WebSocket connections and provides a comfortable abstractions of the protocol to easily send and receive messages. It however becomes uncomfortable if you want to use this module manually.

WebSocketConnection

Exceptions

Keeping a WebSocket connection alive may lead to issues. These can have various reasons, like network errors or server issues. Fortunately, anysocks.exceptions provides a few useful exception classes that give you information about what is going on.

AnySocksError

HandshakeError

ConnectionRejected

ConnectionClosed