module simple_server.simple_server_custom
¶
Short summary¶
module pyrsslocal.simple_server.simple_server_custom
This modules contains a class which implements a simple server.
Classes¶
class |
truncated documentation |
---|---|
Defines a simple handler used by HTTPServer. Firefox works better for local files. This class provides the following … |
|
Defines a thread which holds a web server. |
Functions¶
function |
truncated documentation |
---|---|
pyrsslocal contains some javascript script, it adds the paths to the paths where content will be looked for. |
|
Runs the server. |
Properties¶
property |
truncated documentation |
---|---|
|
A boolean value indicating whether this thread is a daemon thread. This must be set before start() is called, otherwise … |
|
Thread identifier of this thread or None if it has not been started. This is a nonzero integer. See the get_ident() … |
|
A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. … |
|
Native integral thread ID of this thread, or None if it has not been started. This is a non-negative integer. … |
Methods¶
method |
truncated documentation |
---|---|
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request. … |
|
Adds a local path to the list of path to watch. |
|
What to do is case of GET request. |
|
What to do is case of POST request. |
|
Redirection when url is just the website. |
|
Locally execute a python script. |
|
Displays something. |
|
Returns the content of a local file. The function also looks into folders in |
|
Defines the header to send (type of files) based on path. |
|
Returns all the location where the server should look for a java script. |
|
Returns a list of local path where to look for a local file. |
|
Produces a html code for code. |
|
To log, it appends various information about the id address… |
|
Logs an arbitrary message. Overloads the original method. This is used by all other logging functions. Override … |
|
Returns the main page (case the server is called with no path). |
|
To log |
|
Parses a HTML string, extract script section (only python script for the time being) and returns … |
|
Runs the server. |
|
Defines the header to send (type of files) based on path. |
|
Tells what to do based on the path. The function intercepts the path |
|
Functions to overload (executed after serve_content). |
|
Shuts down the service from the service itself (not from another thread). For the time being, the function generates … |
|
Shuts down the server, if it does not work, you can still kill the thread: |
Documentation¶
This modules contains a class which implements a simple server.
- class pyrsslocal.simple_server.simple_server_custom.SimpleHandler(request, client_address, server)¶
Bases:
BaseHTTPRequestHandler
Defines a simple handler used by HTTPServer. Firefox works better for local files.
This class provides the following function associated to
/localfile
:if the url is
http://localhost:port/localfile/<filename>
, it display this fileyou add a path parameter:
http://localhost:port/localfile/<filename>?path=<path>
to tell the service to look into a different folderyou add a parameter
&execute=False
for python script if you want to display them, not to run them.you can add a parameter
&keep
, the class retains the folder and will look further files in this list
Warning
Some information about pathes are stored in a unique queue but it should be done in cookie or in session data. An instance of SimpleHandler is created for each session and it is better to assume you cannot add member to this class.
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request.
- LOG(*args)¶
To log, it appends various information about the id address…
- Parameters:
args – string to LOG or list of strings to LOG
- __init__(request, client_address, server)¶
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request.
- add_path(p)¶
Adds a local path to the list of path to watch.
- Parameters:
p – local path to data
Python documentation says list are proctected against multithreads (concurrent accesses).
- do_GET()¶
What to do is case of GET request.
- do_POST()¶
What to do is case of POST request.
- do_redirect(path='/index.html')¶
Redirection when url is just the website.
- Parameters:
path – path to redirect to (a string)
- execute(localpath)¶
Locally execute a python script.
- Parameters:
localpath – local python script
- Returns:
output, error
- feed(any_, script_python=False, params=None)¶
Displays something.
- Parameters:
any – string
script_python – if True, the function processes script sections
params – extra parameters, see @me process_scripts
A script section looks like:
<script type="text/python"> from pandas import DataFrame pars = [ { "key":k, "value":v } for k,v in params ] tbl = DataFrame (pars) print ( tbl.tohtml(class_table="myclasstable") ) </script>
- get_file_content(localpath, ftype, path=None)¶
Returns the content of a local file. The function also looks into folders in
self.__pathes
to see if the file can be found in one of the folder when not found in the first one.- Parameters:
localpath – local filename
ftype – r or rb
path – if != None, the filename will be path/localpath
- Returns:
content
- get_ftype(path)¶
Defines the header to send (type of files) based on path.
- Parameters:
path – location (a string)
- Returns:
htype, ftype (html, css, …)
- get_javascript_paths()¶
Returns all the location where the server should look for a java script.
- Returns:
list of paths
- get_pathes()¶
Returns a list of local path where to look for a local file.
- Returns:
a list of pathes
- html_code_renderer(localpath, content)¶
Produces a html code for code.
- Parameters:
localpath – local path to file (local or not)
content – content of the file
- Returns:
html string
- log_message(format, *args)¶
Logs an arbitrary message. Overloads the original method.
This is used by all other logging functions. Override it if you have specific logging wishes.
The first argument, FORMAT, is a format string for the message to be logged. If the format string contains any % escapes requiring parameters, they should be specified as subsequent arguments (it’s just like printf!).
The client ip and current date/time are prefixed to every message.
- main_page()¶
Returns the main page (case the server is called with no path).
- Returns:
default page
- private_LOG(*s)¶
To log
- Parameters:
s – string to LOG or list of strings to LOG
- process_scripts(content, params)¶
Parses a HTML string, extract script section (only python script for the time being) and returns the final page.
- Parameters:
content – html string
params – dictionary with what is known from the server
- Returns:
html content
- send_headers(path)¶
Defines the header to send (type of files) based on path.
- Parameters:
path – location (a string)
- Returns:
type (html, css, …)
- serve_content(path, method='GET')¶
Tells what to do based on the path. The function intercepts the path
/localfile/
, otherwise it callsserve_content_web
.If you type
http://localhost:8080/localfile/__file__
, it will display this file.- Parameters:
path – ParseResult
method – GET or POST
- serve_content_web(path, method, params)¶
Functions to overload (executed after serve_content).
- Parameters:
path – ParseResult
method – GET or POST
params – params parsed from the url + others
- shutdown()¶
Shuts down the service from the service itself (not from another thread). For the time being, the function generates the following exception:
Traceback (most recent call last): File "simple_server_custom.py", line 225, in <module> run_server(None) File "simple_server_custom.py", line 219, in run_server server.serve_forever() File "c:\python33\lib\socketserver.py", line 237, in serve_forever poll_interval) File "c:\python33\lib\socketserver.py", line 155, in _eintr_retry return func(*args) ValueError: file descriptor cannot be a negative integer (-1)
A better way to shut it down should is recommended. The use of the function:
self.server.shutdown()
freezes the server because this function should not be run in the same thread.
- class pyrsslocal.simple_server.simple_server_custom.ThreadServer(server)¶
Bases:
Thread
Defines a thread which holds a web server.
attribute
meaning
server
the server of run
- Parameters:
server – to run
- __init__(server)¶
- Parameters:
server – to run
- run()¶
Runs the server.
- shutdown()¶
Shuts down the server, if it does not work, you can still kill the thread:
self.kill()
- pyrsslocal.simple_server.simple_server_custom.get_path_javascript()¶
pyrsslocal contains some javascript script, it adds the paths to the paths where content will be looked for.
- Returns:
a path
- pyrsslocal.simple_server.simple_server_custom.run_server(server, thread=False, port=8080)¶
Runs the server.
- Parameters:
server – if None, it becomes
HTTPServer(('localhost', 8080), SimpleHandler)
thread – if True, the server is run in a thread and the function returns right away, otherwite, it runs the server.
port – port to use
- Returns:
server if thread is False, the thread otherwise (the thread is started)
Warning
If you kill the python program while the thread is still running, python interpreter might be closed completely.