Python Web Server Content and Problems

The Python Web Server support referred to in this article is currently in Alpha and is not ready for use in public courses. Before using it, you should discuss your use case with Grok staff so that we can figure out the best way to support you. We are not able to provide support for this feature without prior agreement.

The Python 3 (Web Server) language allows users to run Web Servers inside Grok and expose them (temporarily) to the public internet. It also allows Authors to mark Web Servers by making requests to these servers within our sandbox. These Web Servers have the same sandbox limitations as regular Python programs and will terminate if they hit their limits.

We recommend using the Flask library to build web servers inside Grok, and the requests library to test them. You may also be interested in allowing HTTP requests from within the sandboxer if you want your students code to make requests to the internet.

Web Server Examples

To create a Web Server example set the module's default language to Python 3 (Web Server) or use the lang py3_web_server. Then write a regular code snippet. Your program will be run with python and will be available to the internet. Once running our Web Server debug tool will connect to your server and allow you to use it.

The simplest way to do this is using Flask, which has a custom integration. Here is an example running with flask:

You can see the server program in the first file. When run, it outputs regular output via a terminal, and then provides a web viewer that connects to the server.

Any Python server can be setup to run using the Web Server support, as long as it is already installed in our sandbox. To allow access to the public internet the server must use the environment variables we export for IP and PORT. An example of a static file server is provided below:

Web Server Marking

To mark web servers we provide a driver that will run the web server in one thread, then run your marking code in another thread. Because the two threads are running within the same sandbox, your marking code is able to make requests to the student's server. Select Python 3 Web Server as the driver and Output JSON Checker as the output checker. When you create a test, make sure the file type is a Web server test script otherwise the test will not run correctly.

Following is an example configuration:

And an example test file:

Making Requests

We recommend using the requests library to make requests. You can find documentation here.

Otherwise you have the ability to run any Python code you could usually run within the Python sandbox. For example you could import the json library to parse JSON, or use the html library to process html. You will not be able to run the result through a web browser however, so you'll need to test any responses without running them as code.

Web Server Marking Functions

The Python 3 Web Server Driver provides a number of helpers as globals. You can call these helpers to perform useful functions. By default the test will be marked as passed, so you will only need to deal with failure cases.

Following is a list of helpers:

def fail(reason=None, error=None)

fail - will fail the test with the given reason. The error parameter should typically not be used, it creates a System Error, which indicates that marking failed because of a fault of the marking code - not the user code.

def debug(message)

debug - adds a message to the debug log. You can view this by adding {debug} to your On Fail message and then failing the test. It is not visible if the test passed.

def get_base_url() 

get_base_url - retrieves the base URL for the server, for you to make requests against. Typically this will be something like http://127.0.0.1:8080/. You can retrieve any path by concatenating this string with your path, e.g. get_base_url() + 'gday.html'. The base URL includes a trailing slash.

def make_diff(actual, expected, escape=True, lang='out')

make_diff - makes a diff output of the actual and expected as a HTML string. The lang is used as the output language, for example if you expect the output to be JSON you could use the json lang. The escape parameter defines whether to escape the output, since it will come out as HTML it is an XSS security risk not to escape the actual and expected content.