Pre-Flight Check

Objective: Pre-Flight Check

Welcome to Pre-Flight Check: Testing Your Game Locally.

Our objective for this micro-quest is to launch a local web server to serve your generated game files and open the game in your web browser to verify that it runs correctly before deploying.

Testing locally using a web server is a crucial step to ensure your web-exported game behaves as expected in a browser environment before you deploy it publicly.

Check Spec: Verification Script

Before we begin implementation, let's review the verification script (test_serve_game.py) that will check our work.

This script contains tests to ensure:

  • The server script file exists.
  • The server script imports the necessary Python modules (http.server, socketserver).
  • The server script contains the core logic to set up and run a simple HTTP server.

We will run this script now to see the initial state.

Implement: Create Local Server Script

Now, let's create the Python script (serve_game.py) that will launch a local web server to serve your game files.

Step by step checklist:

  1. Create a new Python file named serve_game.py.
  2. Add the necessary import statements for the http.server, socketserver, and os modules.
  3. Define a variable for the server port number (e.g., 8000).
  4. Define a variable for the directory to serve files from (use '.' for the current directory).
  5. Change the current working directory of the script to the serving directory.
  6. Set up the handler for serving files using http.server.SimpleHTTPRequestHandler.
  7. Create the server instance using socketserver.TCPServer, binding to all interfaces ('') and the defined port, using the specified handler.
  8. Add print statements to inform the user that the server has started and the address to access it.
  9. Implement a loop to keep the server running indefinitely using .serve_forever().
  10. Add error handling (e.g., a try...except KeyboardInterrupt) to allow the user to stop the server cleanly by pressing Ctrl+C.
  11. Inside the exception handler, print a message indicating the server is stopping and call the server's .shutdown() method.

The following documentation sections are going to be helpful:

  • Local Web Server Testing

Validate: Run Verification Script Again

With the serve_game.py script created, let's run the verification script (test_serve_game.py) again to confirm that it meets the specified requirements.

If all tests pass, you have successfully created the script needed to serve your game locally. The next step would be to run python serve_game.py in your terminal from the directory containing your exported game files and then open http://localhost:8000/ (or the port you chose) in your web browser.

Verification Results:

  • [Results will be displayed here]

Documentation

Documentation not available for PY-1.4-BP5.