Introducing Python websockets and asyncio with a worked example

Cassius
Cassandra
Published in
4 min readDec 30, 2020

--

A quick beginner’s tutorial into Python websockets with a Deribit cryptocurrency example.

Photo by Stefan Gall on Unsplash

In this note, we’re going to run through some basics on running Python websockets, along with an example where we use websockets to receive live cryptocurrency data from one the largest Bitcoin futures trading platform Deribit.

Firstly, you need to install websockets.

Installing Python websockets

Installing websockets:

pip install websockets

Websockets requires Python 3.6.1 or greater. Perhaps you can create a new virtual environment and install websockets into this environment. This way, you don’t mess around with your base environment.

conda create --name myNewEnv python=3.7
conda install -n myNewEnv websockets

In the code above, we’ve created an new Python virtual environment called “myNewEnv” in which we install Python version 3.7. We have also installed websockets into this version.

What is websockets?

Websockets is a standard protocol for two way data transfer between a client and a server. Websockets does not run over HTTP, it has a separate implementation on top of TCP. This is the…

--

--

Cassius
Cassandra

writes about machine learning and programming.