producti_gestio.server package¶
producti_gestio.server.server module¶
producti_gestio.server¶
That’s the server part of the module. It contains the main class: the server class. It could generate a new server based on a configuration and a function.
producti_gestio.server.server¶
This section contains the producti_gestio.server.server.Server
class, the main class
of the entire library.
It could create the web-server, set the configuration by the init function or using set_* functions.
You can create handlers (using producti_gestio.server.server.Server.on_request()
or not)
and start using it simply using producti_gestio.server.server.Server.run()
or
producti_gestio.server.server.Server.start()
for who’s gonna use Threads.
The producti_gestio.Server
allows a custom configuration that you
can pass when you create a new instance as kwargs
:
- allow_get (
bool
): If you want to allowGET
requests. Default is False- allow_post (
bool
): If you want to allowPOST
requests. Default is True- function (
callable
): Yourhandler_function
, it won’t be considered if you’re using Handlers.- debug (
bool
): If you want to get debugging infos- ip* (
str
): Yourip
. Default is 127.0.0.1- port (
int
): Yourserver post
. Default is 8000
Here an example:
from producti_gestio import Server
my_server = Server(allow_get=True, allow_post=False) # Create the Server instance
def my_function(**kwargs):
return {
'response_code': 200,
'response': {
'ok': True,
'message': 'Hello world!'
}
}
my_server.set_function(my_function) # Set the handler function
my_server.start() # Run the server using threads
while True:
pass
Or using producti_gestio.handlers.handler
and producti_gestio.filters.filter
:
from producti_gestio import Server, Filter
my_server = Server(allow_get=True, allow_post=False) # Create the Server instance
@my_server.on_request(Filter.get) # Define the decorator for the function (it uses Filters)
def my_function(**kwargs): # Define the handler function
return {
'response_code': 200, # The response code
'response': { # A dict of the response (it will be encoded in JSON)
'ok': True,
'message': 'Hello world!'
}
}
my_server.start() # Run the server using threads
while True:
pass
-
class
producti_gestio.server.server.
Server
(**conf)¶ Bases:
object
The main class. It is used to create, edit configuration and launch the web server.
-
__init__
(**conf)¶ It defines the configuration by merging the default configuration with the user-chosen configuration.
Parameters: conf (configuration) – The chosen configuration. See producti_gestio.server.server
.
-
__repr__
() → str¶ It returns a representation of the object.
Returns: A representation of the object Return type: str
-
add_handler
(handler: object) → bool¶ It adds an handler to the server object.
Here an example:
from producti_gestio import Server from producti_gestio.handlers.handler import Handler my_server = Server(allow_get=True) # Create the Server instance. my_handler = Handler(lambda parameters: bool(parameters['path'] == '/test')) # Create the handler my_server.add_handler(my_handler) # Add the handler to the Server object my_server.start() # Run the server using threads
Parameters: - handler (Handler) – The Handler
- handler – object:
Returns: True if all went right, False on fails
Return type: bool
-
on_request
(filters: object = None) → callable¶ It adds the function to the handler.
It could be used as a decorator, here an example:
from producti_gestio import Server, Filters my_server = Server(allow_get=True) # Create the Server instance @my_server.on_request(Filters.get) # Create and add an handler using Filters der my_function(**kwargs): return { 'response_code': 200, # The response code 'response' : { 'ok': True } } my_server.start() # Start the server using Threads
Parameters: - filters (Filter) – The filters you’d like to use
- filters – object or None: (Default value = None)
Returns: A decorator function
Return type: callable
-
remove_handler
(handler: object) → bool¶ It removes an handler from the server object.
Here an example:
from producti_gestio import Server from producti_gestio.handlers.handler import Handler my_server = Server(allow_get=True) # Create the Server instance. my_handler = Handler(lambda parameters: bool(parameters['path'] == '/test')) # Create the handler other_handler = Handler(lambda parameters: bool(parameters['path'] == '/second_test')) # Create the other handler my_server.add_handler(my_handler) # Add the handler to the Server object my_server.start() # Start the server using threads my_server.add_handler(other_handler) # Add the other handler to the Server object my_server.remove_handler(my_handler) # Remove 'my handler' from the Server object
Parameters: - handler (Handler) – The Handler
- handler – object:
Returns: True if all went right, False on fails
Return type: bool
-
run
(handler_class=<class 'producti_gestio.core.request_handler.RequestHandler'>)¶ It launches the server and add the configuration to the Handler class.
Remember: It doesn’t use
_thread
, seeproducti_gestio.server.server.Server.start()
for that.Parameters: handler_class (classobj|type, optional) – The Handler class you would like to use. Default is producti_gestio.core.request_handler.RequestHandler
.Raises: NotDefinedFunction
– It throws a NotDefinedFunction exception if the handler function is not defined.
-
set_allow_get
(allow_get: bool) → bool¶ It sets if the GET method is allowed by the allow_get parameter.
Remember: The GET method is not allowed on by default!
Here an example:
from producti_gestio import Server my_server = Server() # Create the server instance @my_server.on_request def my_function(**kwargs): return { 'response_code': 200, # The Response Code 'response': { 'ok': True } } my_server.set_allow_get(True) # Allow GET requests my_server.start() # Start the server using Threads
Parameters: - allow_get (bool) – A boolean of the choice.
- allow_get – bool:
Raises: NotABoolean
– It throws a NotABoolean exception if the given parameter is not a boolean.
-
set_allow_post
(allow_post: bool) → bool¶ It sets if the POST method is allowed by the allow_post parameter.
Remember: The POST method is allowed by default!
Here an example:
from producti_gestio import Server my_server = Server() # Create the server instance @my_server.on_request def my_function(**kwargs): return { 'response_code': 200, # The Response Code 'response': { 'ok': True } } my_server.set_allow_post(True) # Allow POST requests my_server.start() # Start the server using Threads
Parameters: - allow_post (bool) – A boolean of the choice.
- allow_post – bool:
Raises: NotABoolean
– It throws a NotABoolean exception if the given parameter is not a boolean.
-
set_debug_mode
(debug: bool) → bool¶ It sets if the Debug mode is on.
Here an example:
from producti_gestio import Server my_server = Server() # Create the server instance @my_server.on_request def my_function(**kwargs): return { 'response_code': 200, # The Response Code 'response': { 'ok': True } } my_server.set_debug_mode(True) # Enable the debug mode my_server.start() # Start the server using Threads
Parameters: - debug (bool) – A boolean of the choice.
- debug – bool:
Raises: NotABoolean
– It throws a NotABoolean exception if the given parameter is not a boolean.
-
set_function
(handler_function: callable) → bool¶ It sets the function by the handler_function parameter.
Here an example:
from producti_gestio import Server my_server = Server() # Create the server instance def my_function(**kwargs): # It creates the function return { 'response_code': 200, # The Response Code 'response': { 'ok': True } } my_server.set_function(my_function) # Set the handler function my_server.start() # Start the server using Threads
Parameters: - handler_function (callable) – The user-defined function you would like to set.
- handler_function – callable:
Raises: NotAFunction
– It throws a NotAFunction exception if the given parameter is not a function.
-
set_ip
(ip: str) → bool¶ It sets the IP by the IP parameter.
Here an example:
from producti_gestio import Server my_server = Server() # Create the server instance @my_server.on_request def my_function(**kwargs): return { 'response_code': 200, # The Response Code 'response': { 'ok': True } } my_server.set_ip('127.0.0.1') # Set the IP my_server.start() # Start the server using Threads
Parameters: - ip (str) – The IP you chose
- ip – str:
Raises: NotAString
– It throws a NotAString exception if the given parameter is not a string.
-
set_port
(port: int) → bool¶ It sets the Port by the Port parameter.
Here an example:
from producti_gestio import Server my_server = Server() # Create the server instance @my_server.on_request def my_function(**kwargs): return { 'response_code': 200, # The Response Code 'response': { 'ok': True } } my_server.set_port(8000) # Set the Port my_server.start() # Start the server using Threads
Parameters: - port (int) – The Port you chose.
- port – int:
Raises: NotAnInteger
– It throws a NotAnInteger exception if the given parameter is not an integer.
-
shutdown
()¶ It shutdowns the server.
-
start
() → bool¶ It starts the server. If
thread_activated
isTrue
, it starts the server using_thread
, else it just callsproducti_gestio.server.server.Server.run()
.Returns: True if all went right, False on fails Return type: bool
-
stop
()¶ It stops the server, you can re-put it on in by re-calling the run function.
-
thread_activated
= True¶ This variable is used to store if the computer could import the
_thread
library. It is used byproducti_gestio.server.server.Server.start()
to start the server using Threads.
-