Giter VIP home page Giter VIP logo

chat's Introduction

chat

Demo application for asyncio, for teaching purposes.

The goal is to show how to build a non-trivial application, since a common (and justified) criticism of many asyncio tutorials is that they're too simple to be useful for building real-world applications.

image

image

image

How To Run

Requires Python >= 3.7

  1. Open a new terminal, run $ python srv.py
  2. Open a new terminal, run $ python client.py
  3. Open a second new terminal, run $ python client.py

There should be two GUI windows open, and if you type a message into the bottom edit box and hit ENTER, you should see the message appear in the other GUI.

Demonstrated Techniques

This is a chat client/server application, perhaps like IRC or Slack. These are the specific features that are demonstrated in the demo code:

  • server application with many concurrent connections
    • using asyncio.start_server()
  • separate client application that connects to the server. Multiple client instances can be started.
    • using asyncio.open_connection()
    • handle server outage with a reconnection loop
  • new asyncio APIs like asyncio.run() and asyncio.create_task
  • Using the asyncio Streams API
  • decoupling tasks for sending and receiving
  • SSL connections
  • client application has a Tkinter GUI.
    • show how to run the asyncio loop in a separate thread
    • Tkinter UI loop runs in the main thread
    • This is a similar problem you will encounter with many game frameworks
  • Signal handling to control shutdown
    • Linux: SIGINT and SIGTERM
    • Windows: SIGINT (actually CTRL-C) and SIGBREAK
  • Keeping track of current connections/clients, using weakref containers to make it easier to stay up-to-date.
  • Using queues in the client UI application to pass data between the main thread (UI) and the asyncio thread (IO)
  • Creating a message protocol, by using a size prefix, and converting from the stream bytes to an integer using int.frombytes()
  • Passing JSON between client and server
  • A Tkinter GUI app that can be sized and maximised

ssl

See Doug Hellman's block for how to set up the SSL connection. In particular, for generating the cert and key:

$ openssl req -newkey rsa:2048 -nodes -keyout chat.key \
    -x509 -days 365 -out chat.crt

This creates chat.key and chat.crt in the current dir.

chat's People

Contributors

cjrh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.