module simple_server.simple_server_custom

Inheritance diagram of pyrsslocal.simple_server.simple_server_custom

Short summary

module pyrsslocal.simple_server.simple_server_custom

This modules contains a class which implements a simple server.

source on GitHub

Classes

class

truncated documentation

SimpleHandler

Defines a simple handler used by HTTPServer. Firefox works better for local files. This class provides the following …

ThreadServer

Defines a thread which holds a web server.

Functions

function

truncated documentation

get_path_javascript

pyrsslocal contains some javascript script, it adds the paths to the paths where content will be looked for.

run_server

Runs the server.

Properties

property

truncated documentation

daemon

A boolean value indicating whether this thread is a daemon thread. This must be set before start() is called, otherwise …

ident

Thread identifier of this thread or None if it has not been started. This is a nonzero integer. See the get_ident() …

name

A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. …

native_id

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

__init__

Regular constructor, an instance is created for each request, do not store any data for a longer time than a request. …

__init__

add_path

Adds a local path to the list of path to watch.

do_GET

What to do is case of GET request.

do_POST

What to do is case of POST request.

do_redirect

Redirection when url is just the website.

execute

Locally execute a python script.

feed

Displays something.

get_file_content

Returns the content of a local file. The function also looks into folders in self.__pathes to see if the file …

get_ftype

Defines the header to send (type of files) based on path.

get_javascript_paths

Returns all the location where the server should look for a java script.

get_pathes

Returns a list of local path where to look for a local file.

html_code_renderer

Produces a html code for code.

LOG

To log, it appends various information about the id address…

log_message

Logs an arbitrary message. Overloads the original method. This is used by all other logging functions. Override …

main_page

Returns the main page (case the server is called with no path).

private_LOG

To log

process_scripts

Parses a HTML string, extract script section (only python script for the time being) and returns …

run

Runs the server.

send_headers

Defines the header to send (type of files) based on path.

serve_content

Tells what to do based on the path. The function intercepts the path /localfile/, otherwise it calls serve_content_web. …

serve_content_web

Functions to overload (executed after serve_content).

shutdown

Shuts down the service from the service itself (not from another thread). For the time being, the function generates …

shutdown

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.

source on GitHub

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 file

  • you add a path parameter: http://localhost:port/localfile/<filename>?path=<path> to tell the service to look into a different folder

  • you 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

See Python documentation

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.

source on GitHub

Regular constructor, an instance is created for each request, do not store any data for a longer time than a request.

source on GitHub

LOG(*args)

To log, it appends various information about the id address…

Parameters:

args – string to LOG or list of strings to LOG

source on GitHub

__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.

source on GitHub

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).

source on GitHub

do_GET()

What to do is case of GET request.

source on GitHub

do_POST()

What to do is case of POST request.

source on GitHub

do_redirect(path='/index.html')

Redirection when url is just the website.

Parameters:

path – path to redirect to (a string)

source on GitHub

execute(localpath)

Locally execute a python script.

Parameters:

localpath – local python script

Returns:

output, error

source on GitHub

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>

source on GitHub

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

source on GitHub

get_ftype(path)

Defines the header to send (type of files) based on path.

Parameters:

path – location (a string)

Returns:

htype, ftype (html, css, …)

source on GitHub

get_javascript_paths()

Returns all the location where the server should look for a java script.

Returns:

list of paths

source on GitHub

get_pathes()

Returns a list of local path where to look for a local file.

Returns:

a list of pathes

source on GitHub

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

source on GitHub

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.

source on GitHub

main_page()

Returns the main page (case the server is called with no path).

Returns:

default page

source on GitHub

private_LOG(*s)

To log

Parameters:

s – string to LOG or list of strings to LOG

source on GitHub

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

source on GitHub

send_headers(path)

Defines the header to send (type of files) based on path.

Parameters:

path – location (a string)

Returns:

type (html, css, …)

source on GitHub

serve_content(path, method='GET')

Tells what to do based on the path. The function intercepts the path /localfile/, otherwise it calls serve_content_web.

If you type http://localhost:8080/localfile/__file__, it will display this file.

Parameters:
  • path – ParseResult

  • method – GET or POST

source on GitHub

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

source on GitHub

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.

source on GitHub

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

source on GitHub

Parameters:

server – to run

source on GitHub

__init__(server)
Parameters:

server – to run

source on GitHub

run()

Runs the server.

source on GitHub

shutdown()

Shuts down the server, if it does not work, you can still kill the thread:

self.kill()

source on GitHub

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

source on GitHub

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.

source on GitHub