module custom_server.aserver

Inheritance diagram of pyrsslocal.custom_server.aserver

Short summary

module pyrsslocal.custom_server.aserver

This modules contains a class which implements a simple server.

source on GitHub

Classes

class

truncated documentation

CustomDBServer

defines a custom server which includes an access to a database, this database will contain de table to store the clicks …

CustomDBServerHandler

The server proposes a simple way to create one server on your own. It includes an access to a SQLlite3 database.

Static Methods

staticmethod

truncated documentation

run_server

start the server

schema_table

returns the schema for a specific table

Methods

method

truncated documentation

__enter__

What to do when creating the class.

__exit__

What to do when removing the instance (close the log file).

__init__

constructor

__init__

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

get_javascript_paths

returns all the location where the server should look for a java script

interpret_parameter_as_list_int

interpret a list of parameters, each of them is a list of integer separated by ,

main_page

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

process_event

Processes an event, it expects a format like the following:

process_event

process an event, and log it

serve_content_web

functions to overload (executed after serve_content)

Documentation

This modules contains a class which implements a simple server.

source on GitHub

class pyrsslocal.custom_server.aserver.CustomDBServer(server_address, dbfile, RequestHandlerClass=<class 'pyrsslocal.custom_server.aserver.CustomDBServerHandler'>, main_page='index.html', root=None, logfile=None)[source]

Bases: socketserver.ThreadingMixIn, http.server.HTTPServer

defines a custom server which includes an access to a database, this database will contain de table to store the clicks

create a custom local server

The following code creates an instance of a local server. The server expects to find its content in the same folder.

from pyensae import Database

db = Database(dbfile)
df = pandas.DataFrame ( [ {"name":"xavier", "module":"pyrsslocal"} ] )
db.connect()
db.import_dataframe(df, "example")
db.close()

url = "http://localhost:%d/p_aserver.html" % port
webbrowser.open(url)
CustomDBServer.run_server(None, dbfile, port = port, extra_path = os.path.join("."))

The main page is the following one and it can contains a Python script which will be interpreter by the server. It gives access to a variable db which is a local database in SQLlite.

<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<link type="text/css" href="/p_aserver.css" rel="stylesheet"/>
<title>Custom DB Server</title>
<meta content="dupre, pyrsslocal, custom server" name="keywords"/>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<link rel="shortcut icon" href="p_aserver.ico" />
<meta content="CustomServer from pyrsslocal" name="description" />
<script type="text/javascript" src="/p_aserver.js"></script>
<script src="/js/run_prettify.js" type="text/javascript"></script>

</head>

<body onload="setPositions(['divtable', ])" class="mymainbody">

<div class="divtop">
<h1>Custom DB Server unittest</h1>
</div>

<div class="divtable" id="divfiles" onscroll="savePosition('divtable')">

<h2>Content of table example</h2>

<script type="text/python">
print("<table>")
db.connect()
for row in db.execute_view("SELECT * FROM example") :
    srow = [ str(_) for _ in row ]
    print( "<tr><td>{0}</td></tr>".format("</td><td>".join(srow) ) )
db.close()
print("</table>")
</script>

<p>end.</p>

</div>
</body>
</html>

source on GitHub

constructor

Parameters
  • server_address – addess of the server

  • RequestHandlerClass – it should be CustomServerHandler

  • dbfile – database filename (SQLlite format)

  • main_page – main page for the service (when requested with no specific file)

  • root – folder or list of folders where the server will look into for files such as the main page

source on GitHub

__enter__()[source]

What to do when creating the class.

source on GitHub

__exit__(exc_type, exc_value, traceback)[source]

What to do when removing the instance (close the log file).

source on GitHub

__init__(server_address, dbfile, RequestHandlerClass=<class 'pyrsslocal.custom_server.aserver.CustomDBServerHandler'>, main_page='index.html', root=None, logfile=None)[source]

constructor

Parameters
  • server_address – addess of the server

  • RequestHandlerClass – it should be CustomServerHandler

  • dbfile – database filename (SQLlite format)

  • main_page – main page for the service (when requested with no specific file)

  • root – folder or list of folders where the server will look into for files such as the main page

source on GitHub

process_event(event)[source]

Processes an event, it expects a format like the following:

type1/uuid/type2/args
Parameters

event – string to log

source on GitHub

static run_server(server, dbfile, thread=False, port=8080, logfile=None, extra_path=None)[source]

start the server

Parameters
  • server – if None, it becomes CustomServer(dbfile, ('localhost', 8080), CustomServerHandler)

  • dbfile – file to the RSS database (SQLite)

  • 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

  • logfile – file for the log or “stdout” for the standard output

  • extra_path – additional path the server should look into to find a page

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

static schema_table(table)[source]

returns the schema for a specific table

Parameters

table – name (in [“stats”, “event”])

Returns

dictionary

source on GitHub

class pyrsslocal.custom_server.aserver.CustomDBServerHandler(request, client_address, server)[source]

Bases: pyrsslocal.simple_server.simple_server_custom.SimpleHandler

The server proposes a simple way to create one server on your own. It includes an access to a SQLlite3 database.

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

__init__(request, client_address, server)[source]

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

source on GitHub

get_javascript_paths()[source]

returns all the location where the server should look for a java script

Returns

list of paths

source on GitHub

interpret_parameter_as_list_int(ps)[source]

interpret a list of parameters, each of them is a list of integer separated by ,

Parameters

ps – something like params.get("blog_selected")

Returns

list of int

source on GitHub

main_page()[source]

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

Returns

default page

source on GitHub

process_event(st)[source]

process an event, and log it

Parameters

st – string to process

source on GitHub

serve_content_web(path, method, params)[source]

functions to overload (executed after serve_content)

Parameters
  • path – ParseResult

  • method – GET or POST

  • params – params parsed from the url + others

source on GitHub