Use CherryPy add a web interface to a Python script

Poker Variants

Poker comes in many flavors, including draw, stud, and hold 'em. In draw poker, you are dealt five cards. After a round of betting, each player elects to trade in from zero to four cards. After another round of betting, the highest hand wins the pot.

Stud poker deals each player seven cards and has a single round of betting. Each player must make the best hand possible from the seven cards they were dealt.

Hold 'em poker has recently become very popular in home play, casinos, and even televised events. This is the variety that my program emulates. In hold 'em, each player is dealt two cards. Then, three cards are dealt face up in the middle of the table. Each player (mentally) makes the best hand possible by combining their hand with the cards on the table. After a round of betting, a fourth card is added to the table; then, there's another round of betting. This process happens once more for a total of five cards on the table and one final round of betting. Players that haven't folded then show their hands, and the winner is determined.

CherryPy

CherryPy [2] is Raspberry Pi's web-savvy cousin. It's a Python module that allows any Python class to become a web server. Class methods decorated with @cherrypy.expose become functions accessible at http://localhost:8080/<functionName>. The method is expected to return an HTML string, which becomes the output sent to the browser.

Sending data to a CherryPy function is just as easy. Method properties become their equivalently named GET or POST variables. If a variable is missing or the function otherwise fails to complete, then the standard Python error output will be returned to the browser.

CherryPy allows easy customization, so you can set your own hostname and port – two of the most commonly requested setup changes. It's also possible to set up a directory as a traditional web server that simply returns static files. This is handy to serve a web framework or to set up an initial environment with JavaScript and then use Ajax functions to communicate with CherryPy and Python.

If your program is entirely accessed via its web functions, then you can start it by saying

cherrypy.quickstart ( myWebClass() )

Visiting http://localhost:8080/helloWorld in your browser will now yield whatever myWebClass()'s helloWorld function returns.

In my Poker program, however, I want to draw on the screen, shuffle cards, and generally play the game while the web browser runs in the background. I do this through threading. If I call

cherrypy.quickstart via start_new_thread

it runs concurrently with my main program. The call to start_new_thread returns immediately, and my main thread can continue what it's doing while the web server runs in the background. By passing a reference to my poker class when I start my web class, web events can interact with the game.

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