module custom_server.aserver
¶
Short summary¶
module pyrsslocal.custom_server.aserver
This modules contains a class which implements a simple server.
Classes¶
class |
truncated documentation |
---|---|
defines a custom server which includes an access to a database, this database will contain de table to store the clicks … |
|
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 |
---|---|
start the server |
|
returns the schema for a specific table |
Methods¶
method |
truncated documentation |
---|---|
What to do when creating the class. |
|
What to do when removing the instance (close the log file). |
|
constructor |
|
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request. … |
|
returns all the location where the server should look for a java script |
|
interpret a list of parameters, each of them is a list of integer separated by , |
|
returns the main page (case the server is called with no path) |
|
Processes an event, it expects a format like the following: |
|
process an event, and log it |
|
functions to overload (executed after serve_content) |
Documentation¶
This modules contains a class which implements a simple server.
- class pyrsslocal.custom_server.aserver.CustomDBServer(server_address, dbfile, RequestHandlerClass=<class 'pyrsslocal.custom_server.aserver.CustomDBServerHandler'>, main_page='index.html', root=None, logfile=None)¶
Bases:
ThreadingMixIn
,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>
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
- __enter__()¶
What to do when creating the class.
- __exit__(exc_type, exc_value, traceback)¶
What to do when removing the instance (close the log file).
- __init__(server_address, dbfile, RequestHandlerClass=<class 'pyrsslocal.custom_server.aserver.CustomDBServerHandler'>, main_page='index.html', root=None, logfile=None)¶
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
- process_event(event)¶
Processes an event, it expects a format like the following:
type1/uuid/type2/args
- Parameters:
event – string to log
- static run_server(server, dbfile, thread=False, port=8080, logfile=None, extra_path=None)¶
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.
- static schema_table(table)¶
returns the schema for a specific table
- Parameters:
table – name (in [“stats”, “event”])
- Returns:
dictionary
- class pyrsslocal.custom_server.aserver.CustomDBServerHandler(request, client_address, server)¶
Bases:
SimpleHandler
The server proposes a simple way to create one server on your own. It includes an access to a SQLlite3 database.
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request.
- __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.
- get_javascript_paths()¶
returns all the location where the server should look for a java script
- Returns:
list of paths
- interpret_parameter_as_list_int(ps)¶
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
- main_page()¶
returns the main page (case the server is called with no path)
- Returns:
default page
- process_event(st)¶
process an event, and log it
- Parameters:
st – string to process
- 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