Use CherryPy add a web interface to a Python script

Code Walkthrough

The Poker program is divided up into a few classes to handle all of the required tasks. Some classes are relatively minor so that variables can be stored in named properties. Others focus on specific game logic, graphics, or the web. I'll go through a lot of the code here, but it's too long to cover in its entirety. You can download all the code for this article from the Raspberry Pi Geek FTP site [3].

The Imports

Imports are Python's way of bringing in external libraries. Here are the libraries Poker uses:

  • random – Shuffles the deck.
  • pygame – Draws the poker table on the Pi.
  • cherrypy – Provides the web framework for individual player's hands to appear on their phone or tablet.
  • thread – Runs the web framework in a separate process so that the main program can continue to run in a non-blocking mode.
  • hashlib – Calculates MD5 hashes. In Poker, it is used to mask a player's name in web traffic.
  • subprocess – Allows Python to interact with the underlying OS. Here I use it to get the IP address and SSID currently associated with the Rasp Pi.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content