How To Build WebSocket Server And Client in Python

What is WebSocket?

A WebSocket is a client-server connection that remains open throughout time.

Through a single TCP/IP socket connection, WebSockets enable a bidirectional, full-duplex communications channel that functions over HTTP.


Python is an interpreted high-level, general-purpose programming language. The use of considerable indentation in its design philosophy emphasizes code readability. Its language elements and object-oriented approach are aimed at assisting programmers in writing clear, logical code for both small and large-scale projects.

Building a WebSocket Server With Python

Let’s first set up the environment:

  1. Make sure you have python installed in your system.
  2. Now use pip to install the WebSocket package using the command below:

We can start building the server and a client to connect once you’ve installed the WebSockets package.

Now Let’s create a Server:

Create a server file for example “”.

Inside the server file, we import the required packages—in this case, asyncIO, and WebSockets.

Paste the following code for creating a web server:

After that, we create a handler with the parameters WebSocket and path. The URL of the server is represented by the WebSocket (localhost:8000).

Then we’ll wait for the message and the incoming connection. We take action based on the information we’ve gathered. A basic response containing the contents of the received data in our example.

Let’s create a WebSocket Client to test the server:

You can also test the WebSocket server using PieSocket’s online websocket tester

Create a file and call it client.html. Inside the file, paste the following code:

Save the file and open it in the browser.

You can see in the console connection is established.

WebSocket Client with Python

Now Let’s create a WebSocket client connection in python.

Create a new File “” and import the packages as we did in our server code.

Now let’s create a Python asynchronous function (also called coroutine).

We will use the connect function from the WebSockets module to build a WebSocket client connection. It returns a WebSocketClientProtocol object, which may be used to transmit and receive WebSocket messages.

You can generate a PieSocket API key from here.

To send the data, we simply call the send coroutine with the string of data we wish to send to the client as input. We’ll send a straightforward “hello” message.

Because this is a coroutine, we’ll use the await keyword to wait for it. Note that using await suspends the current coroutine’s execution (in our example, the test function) until the await completes and provides the result data.

The recv coroutine is then called to receive the data from the client. Let’s see the full code:

Now run your code you will get “hello” as your output.

Hope this helps you with your journey to build a WebSocket Server & Client in Python.

For doing the same in NodeJS, follow our guide: How To Build WebSocket Server And Client in NodeJS