When Python 3.4 was released, I was so happy with the Asyncio integration that I wrote a chat server here.

Time passed and new versions of Python were released. So I decided to migrate the server to Python 3.6.

One of the big changes that occurred in Python 3.5 was support for async and await to replace @asyncio.coroutine and yield from respectively. This small change alone makes the code much easier to read, which has become lighter. But one of the main changes in Python 3.6 are f-strings that make it easy to form messages.

First, let’s prepare the environment. You need to install Python 3.6. If you use Windows, just download the package from Python.org.

Ubuntu 16.10#

If you are using Ubuntu 16.10, you still need to download the sources and compile… but following a suggestion of friends on Telegram, I decided to try with pyenv!

To install on Ubuntu, download the install_python360.sh

and run with:

    bash install_python360.sh

Some packages need to be installed on Ubuntu, so it will use sudo. Be prepared to enter your password. In my case, as I use Docker (docker run -rm -t -i ubuntu:16.10 /bin/bash), I ran the script with root access. If you install in your user account, it will call sudo when necessary. I recorded a small video of what happened during my installation:

Windows#

After installing Python 3.6.0, install websockets with pip3 install websockets

Other systems#

Install Python 3.6.0 and the websockets module.

The new server#

Changing @asyncio.coroutine to async def, the code becomes clearer. In a second pass, I replaced yield from with await. As we are using Python 3.6, it’s worth adapting strings to f-strings. And finally, to complete the migration, I configured the log so that the code doesn’t get cluttered with prints! It ended up like this:

Before running, you need to prepare an SSL certificate (on Linux).

    openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes

The client#

Today we can’t escape Javascript. I made a few changes in the code, the biggest being cosmetic and now the text scrolls automatically when new messages arrive.

Running#

Assuming you are in the same directory as the files of this post, let’s create a simple web server with python, of course:

python -m SimpleHTTPServer 8080

Leave it running and open another terminal. Let’s run our server:

python server.py

And finally, open the browser using localhost or your IP:
http://localhost:8080/cliente.html

Note: As we used a self-signed certificate, you need to give permission to the browser to access the page. Since the websocket only uses SSL, open another window in the browser, but on port 8765:
https://localhost:8765

Follow your browser’s procedure to allow the page to be accessed. Normally you should click a button saying that you want to continue accessing the page. If everything goes well, you will receive the message: Invalid request. Close the window and reload the client in:
http://localhost:8080/cliente.html

It should now have connected normally. Open another window on the same address.
Type:

/nome X

and then send a message. It should appear in the other window. You must enter /nome Nome before sending messages. Test with multiple clients, modify and enjoy!