{ "info": { "author": "Maurizio Manetti", "author_email": "maurizio@imanetti.net", "bugtrack_url": null, "classifiers": [], "description": "RPiHTTPServer\n=============\n\nHTTP server and request handler built on top of Python standard\nlibrary's BaseHTTPServer. Originally intended for Raspberry Pi projects\nwith a web interface, the small web server and the associated request\nhandler add some interesting features to BaseHTTPServer and can be used\nindependently of Raspberry Pi.\n\nThe provided example shows how to create a simple web interface to\nswitch ON / OFF a LED via Raspberry GPIO.\n\nFeatures:\n---------\n\n- config in json file\n- optional multithreaded server\n- static file serving with cache\n- Basic authentication (very basic!)\n- POST parsing\n- QS parsing\n- dynamic routing based on configuration or convention\n- hooks methods to perform differente actions before/after serving a\n request\n\nBasic usage:\n------------\n\nIf you are familiar with Python standard library's BaseHTTPServer and\nBaseHTTPRequestHandler it should be pretty straightforward: add a method\nnamed ``routed_`` to your handler class in order to handle a\nrequest for the ``/`` URL and set the ``self.content`` property\nto the HTML string to be served over HTTP.\n\n.. code:: python\n\n class MyHandler(RPiHTTPRequestHandler):\n def routed_whatever:\n # your method definition to serve http://:/whatever\n # do cool stuff\n self.content = \"whatever\"\n\n MyServer = RPiHTTPServer(path_to_config_file, MyHandler)\n MyServer.serve_forever()\n\nDefault config:\n---------------\n\n.. code:: json\n\n {\n \"SERVER_ADDRESS\": \"0.0.0.0\",\n \"SERVER_PORT\": 8000,\n \"SERVER_MULTITHREADED\": true,\n \"STATIC_URL_PREFIX\": \"/static\",\n \"STATIC_FOLDER\": \"$CWD/static\",\n \"STATIC_CACHE\": 604800,\n \"TEMPLATE_FOLDER\": \"$CWD/templates\",\n \"ROUTE\": {\n \"GET\": {\n \"\": \"default_response\",\n },\n \"POST\": {\n \"\": \"default_response\",\n }\n }\n }\n\nPlease note: ``$CWD`` stands for \"current working directory\" but it\ndefaults to the directory of the config file if it exists.\n\nDetailed instructions\n---------------------\n\nConfiguration file\n~~~~~~~~~~~~~~~~~~\n\nPrepare your config file in JSON format following the format of the\naforementioned default config. Any missing key will be replaced by the\ndefault (e.g.: if you do not specify the port the server will try to\nstart listening on port 80).\n\nAdd whatever configuration additional parameter you may need, for\ninstance ``\"GPIO_PIN\": 5``.\n\nLeave ``\"ROUTE\"`` like it is for the time being (read more about routes\nbelow).\n\nStatic files\n~~~~~~~~~~~~\n\nIf you want to be able to serve static content, such as images, css,\nfonts, javascripts, etc., prepare a folder for such files and put the\nabsolute path in the config parameter ``\"STATIC_FOLDER\"``.\n\nYou can serve static content directly from a subdir named ``\"static\"``\nunder the directory where your python script is: in such case, just omit\nthe ``\"STATIC_FOLDER\"`` parameter in the config file.\n\nThe config parameter ``\"STATIC_URL_PREFIX\"`` identifies the virtual path\nto be prepended in the URL to reach static files from HTTP. So, for\ninstance, if you leave the default ``\"STATIC_URL_PREFIX\"`` and you have\nan image named \"foo.png\" directly under the configured\n``\"STATIC_FOLDER\"``, this will be served via HTTP at\n\n::\n\n http://:/static/foo.png\n\nPython script\n~~~~~~~~~~~~~\n\nNow in your Python script you need to define the logic by extending the\nRPiHTTPRequestHandler class. By default every request to the HTTP server\nis mapped to a method of the extended class with the same name of the\nrequest prepended by ``routed_``.\n\nFor instance, a request to\n``http://:/switchon``, will look for the\nmethod ``routed_switchon`` of the request handler class. If the method\nis not available the server will simply give a 404 error.\n\nIf you want to specify a custom method for a request, define the method\nin the ``\"ROUTE\"`` config parameter. One method you would like almost\ncertainly define (or override) is the ``default_response`` (request for\nthe ``/`` URL).\n\nThe mapped method just need to set the ``self.content`` variable (as a\nstring) and such content will be served over HTTP with content type\n``text/html; charset=UTF-8`` (the default mime type).\n\nIn such scenario, your code could look like this:\n\n.. code:: python\n\n class MyHandler(RPiHTTPRequestHandler):\n\n def routed_switchon(self):\n # DO something cool, e.g.: GPIO.output(self.config[\"GPIO_PIN\"], GPIO.HIGH)\n self.content = \"

Switch on

\"\n\n def routed_switchoff(self):\n # DO something cool, e.g.: GPIO.output(self.config[\"GPIO_PIN\"], GPIO.LOW)\n self.content = \"

Switch off

\"\n\n MyServer = RPiHTTPServer(\"/path/to/config.json\", MyHandler)\n MyServer.serve_forever()\n\nFrom the comments in the above example it should be clear that you can\nhave access to the config parameters via\n``self.config[\"PARAMETER_NAME\"]``. You can also add additional\nproperties to the ``server`` property of the RPiHTTPServer instance,\nthus making them available in the request handler class via\n``self.server.PROPERTY_NAME``. So for instance, referring to the example\nabove you could write:\n\n.. code:: python\n\n MyServer = RPiHTTPServer(\"/path/to/config.json\", MyHandler)\n MyServer.server.switch_status = 0\n MyServer.serve_forever()\n\nNow in MyHandler you can access to ``switch_status`` via\n``self.server.switch_status``.\n\nOther properties you can specify/alter before setting ``self.content``:\n\n- ``self.content_type``: by default set to \"text/html; charset=UTF-8\"\n- ``self.response_status``: integer, by default set to 200\n- ``self.response_headers``: by default an empty dictionary, it will be\n automatically filled with Content-Type and Content-Length before\n sending the response back to the client. Set additional dictionary\n keys to serve additional headers.\n\nOther useful properties accessible in the request handler class:\n\n- ``self.config``: gives you access to the configuration\n- ``self.url``: urlparse result on the request path (see\n https://docs.python.org/2/library/urlparse.html)\n- ``self.qs``: dictionary containing the parameters of the parsed query\n string urlparse.parse\\_qs\n- ``self.form``: cgi.FieldStorage containing the parameters of the\n parsed POST request (see\n https://docs.python.org/2/library/cgi.html#higher-level-interface)\n- ``self.request_xhr``: boolean set to true if the request was issued\n via xhr\n\nAt the current stage the library does not offer support for parametric\nroutes.\n\nHooks\n^^^^^\n\nAvailable hook methods that can be implemented in your extended class:\n\n- ``pre_handle_request``: gets executed before handling the HTTP\n request, that is, before evaluating the route and authentication, but\n after evaluating GET, POST method and parameters sent from the\n client. It doesn't get executed in case of requests to static files.\n- ``pre_call_controller``: gets executed after the controller has been\n established and authentication has been verified, but before calling\n the actual controller method\n- ``pre_serve_response``: gets executed after the controller has been\n executed but before sending any response content back to the client.\n It doesn't get executed in case of 404 error, static files or failed\n authentication.\n- ``post_serve_response``: gets executed after the response has been\n sent back to the client. It doesn't get executed in case of static\n files or failed authentication (but it gets for 404 errors).\n\nHTML templates\n~~~~~~~~~~~~~~\n\nThe library does only offer a very basic template handling. The method\n``render_template`` of the RPiHTTPRequestHandler class expects a\nfilename and a dictionary and set the content to a string. It will look\nfor a file with the specified filename under the folder\n``self.config[\"TEMPLATE_FOLDER\"]`` (if not specified in the config file\nit will default to a folder named \"templates\" under the directory from\nwhich the python script is run). It will then loop the dictionary's keys\nas the strings to be replaced, and the corresponding values as the\nreplacements. Finally, it will set the ``content`` property to the\nresulting string.\n\nThis is an extremely simple and inefficient template's handling: there\nare many better libraries out there (e.g. Jinja2, Pystache) if you want\na better template handling: at the end of the day you have to set the\n``self.content`` variable to the string that will be served over HTTP\n(to serve a default ``text/html`` content-type).\n\nPlease note that UTF-8 will be served by default and currently other\ncharacter-set support is not planned.\n\nTODO\n----\n\n- document how basic auth can be configured\n- support for Python 3k\n- handle config file parse error\n- parametric routes\n- sanitize path in url request\n- handle file upload\n- safely handle non UTF-8 chars in POST request\n\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/mauntrelio/RPiHTTPServer", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "RPiHTTPServer", "package_url": "https://pypi.org/project/RPiHTTPServer/", "platform": "", "project_url": "https://pypi.org/project/RPiHTTPServer/", "project_urls": { "Homepage": "http://github.com/mauntrelio/RPiHTTPServer" }, "release_url": "https://pypi.org/project/RPiHTTPServer/0.4.2/", "requires_dist": null, "requires_python": "", "summary": "HTTP server and request handler built on top of BaseHTTPServer intended for Raspberry Pi projects with a web interface", "version": "0.4.2" }, "last_serial": 3259765, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "b7b505926924b644a786b79322856aeb", "sha256": "1190872f1d533efea1e81defe8c5c33872de3eb8339eedfc4e032e2119374351" }, "downloads": -1, "filename": "RPiHTTPServer-0.0.1.tar.gz", "has_sig": false, "md5_digest": "b7b505926924b644a786b79322856aeb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7029, "upload_time": "2017-03-14T20:28:45", "url": "https://files.pythonhosted.org/packages/1d/db/744b7930d1e7e6f30d98b8490b86bf55a9875e48744091760a62da94ed3e/RPiHTTPServer-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "59fd5d2131aa733ed4b98c5a2aaddf51", "sha256": "a64b5619f89bee6d2f29b7b0eb902be9d07cf2ffc8954ff7cfd544ff3daa8bdd" }, "downloads": -1, "filename": "RPiHTTPServer-0.0.2.tar.gz", "has_sig": false, "md5_digest": "59fd5d2131aa733ed4b98c5a2aaddf51", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8059, "upload_time": "2017-04-17T21:02:05", "url": "https://files.pythonhosted.org/packages/55/ee/d0f59e3d5ee3e2f1e1dacf7d2dbe3d72463b0dfafc4074c194c7411dfe75/RPiHTTPServer-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "f78c59450525a068419d999e1850525e", "sha256": "b5145009c9c5041ea6402281a304d8442ef2d25797e8de6257ccfdf21f6727dc" }, "downloads": -1, "filename": "RPiHTTPServer-0.0.3.tar.gz", "has_sig": false, "md5_digest": "f78c59450525a068419d999e1850525e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8040, "upload_time": "2017-04-17T21:08:19", "url": "https://files.pythonhosted.org/packages/b6/79/7648c0369ef283d1401f6f830e2402875ec7b915a151e092c927a0a6c836/RPiHTTPServer-0.0.3.tar.gz" } ], "0.1.0": [ { "comment_text": "", "digests": { "md5": "3baddefd5c4877f6c4f009660cda92da", "sha256": "7fbc4b11f03b4af1529eb8814576e8f1590344b588cbe8abd91082de6d950475" }, "downloads": -1, "filename": "RPiHTTPServer-0.1.0.tar.gz", "has_sig": false, "md5_digest": "3baddefd5c4877f6c4f009660cda92da", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9031, "upload_time": "2017-04-18T19:39:47", "url": "https://files.pythonhosted.org/packages/ab/2b/a1341dedabdb323a21df6a9a8d24944d0a785a1fc4b97c5dc17614fd610e/RPiHTTPServer-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "15e0d6d01f1712fea8eed6b6bdbff398", "sha256": "3bf0077e539c277298df5613b2a3aa7f7d49bc68cee2b72f46b7448460837553" }, "downloads": -1, "filename": "RPiHTTPServer-0.1.1.tar.gz", "has_sig": false, "md5_digest": "15e0d6d01f1712fea8eed6b6bdbff398", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8309, "upload_time": "2017-05-13T12:33:08", "url": "https://files.pythonhosted.org/packages/9e/3d/787665c07ff8b861f8bdaa7bda6a17ec04136c013abcf5eb9e5b47fb19c8/RPiHTTPServer-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "c995077592516732a3bcc22b5e368d59", "sha256": "b1fdd56c327104a7e9980e9fca851e4dac730bee183c5958fcea643568e6c9f1" }, "downloads": -1, "filename": "RPiHTTPServer-0.1.2.tar.gz", "has_sig": false, "md5_digest": "c995077592516732a3bcc22b5e368d59", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8306, "upload_time": "2017-05-13T12:36:49", "url": "https://files.pythonhosted.org/packages/49/8f/8e4f551d73d3151a9854a21850998dddbab5a8d4e269ca49fe7f2bd8f2fb/RPiHTTPServer-0.1.2.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "8ae5645a4d5b1899178229f05adeb005", "sha256": "c47fb3211cfe6a2dc5d5125e613d12fa3ee57aec9a23ef1abaff68c08c401a00" }, "downloads": -1, "filename": "RPiHTTPServer-0.2.0.tar.gz", "has_sig": false, "md5_digest": "8ae5645a4d5b1899178229f05adeb005", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8267, "upload_time": "2017-05-17T03:42:58", "url": "https://files.pythonhosted.org/packages/dd/9d/d15e623941dbd45c1f89ea2dc980f5c73ba620520d977771a86fb867ae08/RPiHTTPServer-0.2.0.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "0496983da27b001cf4eb7c96c4ba6856", "sha256": "c8c9946d9334f07aa1578eb2e50bc1ce7b70447c28ce44e80f83d6ba574789e2" }, "downloads": -1, "filename": "RPiHTTPServer-0.3.0.tar.gz", "has_sig": false, "md5_digest": "0496983da27b001cf4eb7c96c4ba6856", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9648, "upload_time": "2017-08-01T20:12:44", "url": "https://files.pythonhosted.org/packages/40/30/9e1760790646a41492184d6f389d843f67e1f739ff3440ab73bc2e08cdf7/RPiHTTPServer-0.3.0.tar.gz" } ], "0.3.1": [ { "comment_text": "", "digests": { "md5": "977d8dd4b3f076e10223ee1fc9c45f10", "sha256": "e1147b27df58ca4e43c3fdf2e0121fbf035db3de7c8bcde7cbda4bada0643c59" }, "downloads": -1, "filename": "RPiHTTPServer-0.3.1.tar.gz", "has_sig": false, "md5_digest": "977d8dd4b3f076e10223ee1fc9c45f10", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9680, "upload_time": "2017-08-05T12:57:32", "url": "https://files.pythonhosted.org/packages/6f/2f/551105d40eff88af94edb9441f658d05b47f51977ec375b04e23458147b4/RPiHTTPServer-0.3.1.tar.gz" } ], "0.3.2": [ { "comment_text": "", "digests": { "md5": "0d9e1ab4dacc4c14dfe9a7223e08541c", "sha256": "ccbd892c3a275986211795827f629ab71ec312b0cfe7f22fe3845d6c698e9afc" }, "downloads": -1, "filename": "RPiHTTPServer-0.3.2.tar.gz", "has_sig": false, "md5_digest": "0d9e1ab4dacc4c14dfe9a7223e08541c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9689, "upload_time": "2017-08-05T13:08:06", "url": "https://files.pythonhosted.org/packages/31/02/81f2a69bad50a77f781fce7f05a48369ca5389742d4cd4aa04d8beb60c72/RPiHTTPServer-0.3.2.tar.gz" } ], "0.4.0": [ { "comment_text": "", "digests": { "md5": "223474beaf82c7e87ea9f728a168f4d2", "sha256": "56b0ed74633ffd359321688d1de67051dca341af9a8db129df0fb44d35e1e4fd" }, "downloads": -1, "filename": "RPiHTTPServer-0.4.0.tar.gz", "has_sig": false, "md5_digest": "223474beaf82c7e87ea9f728a168f4d2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9766, "upload_time": "2017-08-05T13:28:54", "url": "https://files.pythonhosted.org/packages/ea/a7/a400d0a99e2e8ea57388e8fbd83acc992912896b9aece60aa7ecb41c6082/RPiHTTPServer-0.4.0.tar.gz" } ], "0.4.1": [ { "comment_text": "", "digests": { "md5": "7430e155e5a51bad27eb55dd4a1e808a", "sha256": "78e961e12321d797557e7b54e168514e9c6663bb9c9fa522f03ddaab79746625" }, "downloads": -1, "filename": "RPiHTTPServer-0.4.1.tar.gz", "has_sig": false, "md5_digest": "7430e155e5a51bad27eb55dd4a1e808a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9797, "upload_time": "2017-08-05T22:49:45", "url": "https://files.pythonhosted.org/packages/bb/60/5ee83d2566b22789601a53a11d8641c8ada27077b1894c830b60ada52c88/RPiHTTPServer-0.4.1.tar.gz" } ], "0.4.2": [ { "comment_text": "", "digests": { "md5": "963df27490420239e38c7d39d139c5f4", "sha256": "3189dec6b52da8dfda15bf97f0fe78616fe35e5f6c4fb07e615a28eb3acfc106" }, "downloads": -1, "filename": "RPiHTTPServer-0.4.2.tar.gz", "has_sig": false, "md5_digest": "963df27490420239e38c7d39d139c5f4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9261, "upload_time": "2017-10-18T12:34:22", "url": "https://files.pythonhosted.org/packages/f0/8f/f1baa69f4cfe1e1c138bd65896629ba4bd8092f50e8c10abe19ee701ca89/RPiHTTPServer-0.4.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "963df27490420239e38c7d39d139c5f4", "sha256": "3189dec6b52da8dfda15bf97f0fe78616fe35e5f6c4fb07e615a28eb3acfc106" }, "downloads": -1, "filename": "RPiHTTPServer-0.4.2.tar.gz", "has_sig": false, "md5_digest": "963df27490420239e38c7d39d139c5f4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9261, "upload_time": "2017-10-18T12:34:22", "url": "https://files.pythonhosted.org/packages/f0/8f/f1baa69f4cfe1e1c138bd65896629ba4bd8092f50e8c10abe19ee701ca89/RPiHTTPServer-0.4.2.tar.gz" } ] }