Get started¶
anysocks
requires Python 3.5+.
Generally speaking, it is best to use the latest versions
of both, anysocks
and Python. Due to bug-fixes and new
features, anysocks
may run more stable and is more
intuitive for you to use.
Installation¶
There are multiple ways of installing anysocks
:
# The latest release from PyPI
pip install anysocks
# The cutting edge development version from GitHub:
pip install git+https://github.com/clamor-py/anysocks@master#egg=anysocks
Basic example¶
Here’s a simple example on connecting to an echo server:
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)
|
Logging¶
If you’re having issues with your program and don’t understand what anysocks
is doing, enable logging for verbose output.
import logging
logger = logging.getLogger('anysocks')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
The log contains:
Sending and receiving frames (
DEBUG
,WARNING
for unknown frame types)Event dispatch handling (
DEBUG
)Connection openings and closures (
INFO
)