Skip to main content

PyCerver is a pure Python wrapper around the cerver framework

Project description

Cerver Banner

PyCerver is a pure Python wrapper around the cerver framework. It uses the built-in ctypes module to interface with cerver, and provides simple Python classes and wrappers for common cerver functionality. You can check the original project's source code here!

Trying out

Local

You can test out PyCerver without actually installing it. You just need to set up your PYTHONPATH to point to the location of the source distribution package.

export PYTHONPATH=/path/to/py-cerver:$PYTHONPATH

Docker

  1. Buid local development docker image
bash development.sh
  1. Run development image with local source
sudo docker run \
	-it \
	--name pycerver --rm \
	-p 8080:8080 \
	-v /home/ermiry/Documents/py-cerver:/home/pycerver \
	ermiry/pycerver:development /bin/bash
  1. Handle pycerver module
export PYTHONPATH=$pwd:$PYTHONPATH
  1. Run any of the included examples
python3 examples/http/web.py

Getting Started

  1. Import the necessary dependencies
# provides the ability to handle signals to stop the application
import signal
import sys			# to use sys.exit () to exit the program

# needed to interact with the low level cerver framework
import ctypes

# get access to main and HTTP cerver methods
from cerver import *
from cerver.http import *
  1. Create a global Cerver instance reference
# define a global reference to the cerver instance
web_service = None
  1. Define an end method to be used as the callback when catching signals. We can use this method to display the cerver's stats and to correctly dispose all the memory that has been allocated by our application
def end (signum, frame):
  # prints the HTTP cerver stats
  # like how many requests were handled by each route
  http_cerver_all_stats_print (http_cerver_get (web_service))

  # correctly disposes HTTP cerver references
  cerver_teardown (web_service)

  # correctly disposes cerver internal global values.
  # Should be called only once at the very end of the program
  cerver_end ()

  # exit the application with a custom message
  sys.exit ("Done!")
  1. Create a handler method for the GET /test route
# GET /test
@ctypes.CFUNCTYPE (None, ctypes.c_void_p, ctypes.c_void_p)
def test_handler (http_receive, request):
  # creates a HTTP response with a status code and a JSON body
  response = http_response_json_msg (
    HTTP_STATUS_OK, b"Test route works!"
  )

  # prints the generated HTTP response
  http_response_print (response)

  # send the HTTP response to the client
  http_response_send (response, http_receive)

  # correctly disposes a HTTP response and all of its data
  http_response_delete (response)
  1. Define a start method where the Cerver instance will be created and its configuration will be set before starting it
def start ():
  # tells Python to use the global reference
  global web_service

  # creates a new cerver of type CERVER_TYPE_WEB
  # that will bind and listen to port 8080 and with a connection queue of size 10
  web_service = cerver_create_web (b"web-service", 8080, 10)

  # main configuration
  # set the cerver's thpool number of threads
  # This will enable the ability to handle requests using multiple threads
  cerver_set_thpool_n_threads (web_service, 4);
  cerver_set_handler_type (web_service, CERVER_HANDLER_TYPE_THREADS);

  # enable cerver's reusable address flags
  # to avoid errors when binding to the selected port
  cerver_set_reusable_address_flags (web_service, True)

  # HTTP configuration
  # gets a reference to the cerver's internal HTTP structure
  http_cerver = http_cerver_get (web_service)

  # GET /test
  # create a new HTTP route to handle GET requests at /test endpoint
  # using the previously defined test_handler ()
  test_route = http_route_create (
    REQUEST_METHOD_GET, b"test", test_handler
  )

  # register the new test route to the HTTP cerver
  http_cerver_route_register (http_cerver, test_route)

  # start
  # start listening for connections and handling requests
  cerver_start (web_service)
  1. Define the application's entry point where the cerver is initialized and the signals are handled
if __name__ == "__main__":
  # register to the desired signals and sets the end () method to handle them
  signal.signal (signal.SIGINT, end)
  signal.signal (signal.SIGTERM, end)
  signal.signal (signal.SIGPIPE, signal.SIG_IGN)

  # initializes global cerver values
  # Should be called only once at the start of the program
  cerver_init ()

  # prints the base cerver framework version information
  cerver_version_print_full ()

  # prints the pycerver wrapper version information
  pycerver_version_print_full ()

  # calls the previously defined start () method
  start ()

You can also take a look at the official wiki to get better explanations about the included examples and of all the methods that are available in the framework.


Ermiry - Never Stop Creating

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycerver-0.8.7.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycerver-0.8.7-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file pycerver-0.8.7.tar.gz.

File metadata

  • Download URL: pycerver-0.8.7.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for pycerver-0.8.7.tar.gz
Algorithm Hash digest
SHA256 cad1343e2d0cb2696c3594e5677bcbdb3a9067d416a92b6d6daf6355ada919d4
MD5 197a4e57defb82f420ef10af8aa06279
BLAKE2b-256 96c883266f41c3720cf406f7aa2cd68e5b979f83a194d8da0c50ae1afc210571

See more details on using hashes here.

File details

Details for the file pycerver-0.8.7-py3-none-any.whl.

File metadata

  • Download URL: pycerver-0.8.7-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.10

File hashes

Hashes for pycerver-0.8.7-py3-none-any.whl
Algorithm Hash digest
SHA256 bfbcab8e6c4b2920eedc0274087265c09f6e0fdf78987f986f50a5f1971eef34
MD5 1cde2fa9d4f5f20220447dc5af9dcd3e
BLAKE2b-256 c658af9e46ff04ecf67b5bd7509231e465c325473f06f931c0986cdbffb12b41

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page