Package: pycerver
PyCerver is a pure Python wrapper around thecerverframework. 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 codehere!Trying outLocalYou 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.exportPYTHONPATH=/path/to/py-cerver:$PYTHONPATHDockerBuid localdevelopmentdocker imagebashdevelopment.shRundevelopmentimage with local sourcesudodockerrun\-it\--namepycerver--rm\-p8080:8080\-v/home/ermiry/Documents/py-cerver:/home/pycerver\ermiry/pycerver:development/bin/bashHandlepycervermoduleexportPYTHONPATH=$pwd:$PYTHONPATHRun any of the included examplespython3examples/http/web.pyGetting StartedImport the necessary dependencies# provides the ability to handle signals to stop the applicationimportsignalimportsys# to use sys.exit () to exit the program# needed to interact with the low level cerver frameworkimportctypes# get access to main and HTTP cerver methodsfromcerverimport*fromcerver.httpimport*Create a globalCerverinstance reference# define a global reference to the cerver instanceweb_service=NoneDefine anendmethod 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 applicationdefend(signum,frame):# prints the HTTP cerver stats# like how many requests were handled by each routehttp_cerver_all_stats_print(http_cerver_get(web_service))# correctly disposes HTTP cerver referencescerver_teardown(web_service)# correctly disposes cerver internal global values.# Should be called only once at the very end of the programcerver_end()# exit the application with a custom messagesys.exit("Done!")Create a handler method for theGET /testroute# GET /test@ctypes.CFUNCTYPE(None,ctypes.c_void_p,ctypes.c_void_p)deftest_handler(http_receive,request):# creates a HTTP response with a status code and a JSON bodyresponse=http_response_json_msg(HTTP_STATUS_OK,b"Test route works!")# prints the generated HTTP responsehttp_response_print(response)# send the HTTP response to the clienthttp_response_send(response,http_receive)# correctly disposes a HTTP response and all of its datahttp_response_delete(response)Define a start method where theCerverinstance will be created and its configuration will be set before starting itdefstart():# tells Python to use the global referenceglobalweb_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 10web_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 threadscerver_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 portcerver_set_reusable_address_flags(web_service,True)# HTTP configuration# gets a reference to the cerver's internal HTTP structurehttp_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 cerverhttp_cerver_route_register(http_cerver,test_route)# start# start listening for connections and handling requestscerver_start(web_service)Define the application's entry point where thecerveris initialized and the signals are handledif__name__=="__main__":# register to the desired signals and sets the end () method to handle themsignal.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 programcerver_init()# prints the base cerver framework version informationcerver_version_print_full()# prints the pycerver wrapper version informationpycerver_version_print_full()# calls the previously defined start () methodstart()You can also take a look at the officialwikito get better explanations about the included examples and of all the methods that are available in the framework.Ermiry - Never Stop Creating