Fujian Documentation¶
Handler Classes¶
-
class
fujian.__main__.
FujianHandler
(application, request, **kwargs)¶ Connect with clients via HTTP.
-
get
()¶ Reply with an empty response body. Basically this is a ping request.
-
post
()¶ Execute Python code submitted in the request body, and return results of the computation.
Note
The global
fujian_return
variable is set to a zero-length string before any code is executed, so it is guaranteed not to contain data from a previous request.Response Body
If the response code is 200, it’s a JSON object with three members:
stdout
,stderr
, andreturn
. If the response code is 400 (meaning there was an unhandled exception) the object also contains atraceback
member.All of these are strings. The
stdout
andstderr
members are the contents of the corresponding stdio streams. Thereturn
member is the value stored in the globalfujian_return
variable at the end of the call. If present,traceback
contains the traceback of the most recent unhandled exception.
-
set_default_headers
()¶ Set the “Server” and “Access-Control-Allow-Origin” response headers.
-
-
class
fujian.__main__.
FujianWebSocketHandler
(*args, **kwargs)¶ Connect with clients via WebSocket.
-
check_origin
(origin)¶ If supplied, Tornado uses this method to allow checking the Origin request header, to verify that the request is indeed coming from somewhere we are okay with. For us that means localhost, with or without HTTPS.
-
is_open
()¶ Determine whether the WebSocket connection is currently open.
If there is no WebSocket currently open, any calls to
write_message()
will raise atornado.websocket.WebSocketClosedError
.
-
on_close
()¶ Set the local flag to know the connection is closed.
-
on_message
(message)¶ Execute Python code submitted in a WebSocket message.
This works much like
post()
except this method will not necessarily produce a response to the client. If the code writes tostdout
orstderr
, or sets the globalfujian_return
variable, a JSON response will be sent to the client in the same way aspost()
. If the code execution raises an unhandled exception, atraceback
member will be included, in the same way aspost()
.If the code does not raise an unhandled exception, write to
stdout
orstderr
, or set the globalfujian_return
variable, no message will be sent to the client about the success or failure of code execution.Furthermore, and quite unlike a connection to
FujianHandler
, messages may be sent to the client without first being requested. This is caused by any code that callswrite_message()
on the globalFUJIAN_WS
object installed byFujianWebSocketHandler
.
-
open
(**kwargs)¶ Set the flag that avoids delaying small messages to save bandwidth. Since Fujian is intended only for use on
localhost
, any delay would be detrimental to the user experience, and morever there is no reason to save bandwidth.Also set the local flag to know the connection is open.
-
Helper Classes¶
-
class
fujian.__main__.
StdoutHandler
¶ This is a replacement for
sys.stdout()
andsys.stderr()
that collects its output for retrieval withget()
.This class supports the required
write()
method for an output stream, but no output is emitted under any circumstances except as returned from theget()
method.
Helper Functions¶
-
fujian.__main__.
make_new_stdout
()¶ Make a new stdout and stderr, with the request’s exec_globals, for a single request.
-
fujian.__main__.
execute_some_python
(code)¶ Execute some Python code in the “exec_globals” namespace.
Parameters: code (str) – The Python code to execute. Returns: A dictionary with “stdout”, “stderr”, “return”, and possibly “traceback” keys. Return type: dict The dictionary returned contains the values written to stdout and stderr during the code execution. If a value is written to the global
fujian_return
variable, that is returned as the value of the “return” key. If the code raises an unhandled exception, the traceback appears is the value of the “traceback” key.Note
All values in the dictionary are guaranteed to be the Unicode string type appropriate to the Python version in use.
-
fujian.__main__.
get_from_stdout
()¶ Get what was written to stdout, with the request’s exec_globals, in this request.
-
fujian.__main__.
get_from_stderr
()¶ Get what was written to stderr, with the request’s exec_globals, in this request.
-
fujian.__main__.
get_traceback
()¶ Get a traceback of the most recent exception raised in the subinterpreter.
-
fujian.__main__.
myprint
(this)¶ Prints “this” using the original stdout, even when it’s been replaced. For use in debugging
fujian
itself.