bbwebservice


Namebbwebservice JSON
Version 1.0.5 PyPI version JSON
download
home_pageNone
SummaryA bare bone webserver
upload_time2024-04-03 16:50:28
maintainerNone
docs_urlNone
authorLukas
requires_python>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # bbwebservice

`bbwebservice` is a small Python library for creating simple webservers.

## Installation

To install this library, use the pip command: `pip install bbwebservice`

## Usage

- how to import:

```python
from bbwebservice.webserver import * 
```

1. Register pages for HTTP `GET`:
   - `@register`: When using the `@register` decorator, you must define the named parameters `route` and `type`.
     - `route`: The URL which must be requested via HTTP `GET` method for the decorated function to execute. The decorated function is expected to return the respective response content.
     - `type`: The `type` parameter holds the intended MIME-type of the response.

```python
@register(route='/', type=MIME_TYPE.HTML)
def main_page():
    return load_file('/content/index.html')
```

2. Register pages for HTTP `POST`:
   - `@post_handler`: Works similarly to [1.], with the only difference being that it is mandatory for the decorated function to take a parameter.

```python
@post_handler(route='/makethread', type=MIME_TYPE.HTML)
def makethread(args):
    return load_file('/content/post.html')
```

3. Redirect:
  To redirect a request, you can return a `Redirect` object that specifies the path to which the request should be redirected.

```python
@register(route='/foo', type=MIME_TYPE.HTML)
def redirect():
    return Redirect('/')
```

4. PartialContent and videostreaming:
   To serve partial-content for video streaming or other applications, you can return a `PartialContent` object which takes the location of the streamed file and the chunk size which determines the size of the parts that should be streamed.

```python
@register(route='/video.mp4', type=MIME_TYPE.MP4)
def vid(args):
    return PartialContent("/content/v.mp4", 80000)
```

5. Error handler:
   With the `@error_handler`, it is possible to provide default responses for requests with the specified error code.

```python
@error_handler(error_code=404, type=MIME_TYPE.HTML)
def not_found():
    return load_file('/content/404.html')
```

6. Handler args:
   Setting cookies, getting query strings, or setting custom headers are possible when you give your handler function a parameter.

```python
@post_handler(route='/', type=MIME_TYPE.HTML)
def login(args):
    set_cookie(args, 'id', "test")
    return load_file('/content/index.html')
```

The server-supplied `args` value looks something like this, and changes to the provided value will be reflected in the server answer:

```py
{
    "query_string": {},
    "flags": [],
    "template_args": {},
    "cookies": {},
    "address": ("192.168.56.1", 64361),
    "post": bytearray(b""),
    "request_header": {
        "Host": ["192.168.56.1:5000"],
        "Connection": ["keep-alive"],
        "User-agent": ["Mozilla/5.0 (Windows NT 10.0", "Win64", "x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"],
        "Accept": ["text/html,application/xhtml+xml,application/xml", "q=0.9,image/avif,image/webp,image/apng,*/*", "q=0.8,application/signed-exchange", "v=b3", "q=0.7"],
        "Accept-encoding": ["gzip, deflate"],
        "Accept-language": ["de-DE,de", "q=0.9,en-US", "q=0.8,en", "q=0.7"]
    },
    "response": "<bbwebservice.http_parser.HTTP_Response object at 0x00000151D5718E50>"
}
```

7. Start the server (example of fully functional server):
   To start the server, invoke the `start` function.

```python
from bbwebservice.webserver import *

@register(route='/', type=MIME_TYPE.HTML)
def main():
    return load_file('/content/index.html')

start()
```

8. With URL-templating you are able to match dynamic URLs like `/test/paul/1456379827256`

```py
@register(route= UrlTemplate('/test/{name:str}/{id:int}'), type=MIME_TYPE.TEXT)
def test(args):
    return str(args[STORE_VARS.TEMPLATE_VARS])

```

## Server Configuration

In the directory `/config`, there is a file named `config.json`. Here you can configure the server:

```json
{
    "ip": "localhost",
    "port": 5000,
    "queue_size": 10,
    "SSL": false,
    "cert_path" : "",
    "key_path" : ""
}
```

If you intend to keep the application centrally (online), it is recommended to set the value of `ip` to either `default` or the IP address of the respective server. Additionally, it is advisable to activate `SSL` and set the corresponding paths for `cert_path` and `key_path`. 

### Recommended Ports

- 5000: For testing purposes
- 443: For HTTPS (SSL = true)
- 80: For HTTP (SSL = false)

## Logging

```python
log_to_file()
set_logging(LOGGING_OPTIONS.INFO, True)
set_logging(LOGGING_OPTIONS.TIME, True)
set_logging(LOGGING_OPTIONS.DEBUG, True)
set_logging(LOGGING_OPTIONS.ERROR, True)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "bbwebservice",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Lukas",
    "author_email": "lukasogwalker@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/3c/86/391ab079f24f5924240a5579304c874a8d1aedd59c88b32d1f374cfb23ab/bbwebservice-1.0.5.tar.gz",
    "platform": null,
    "description": "# bbwebservice\r\n\r\n`bbwebservice` is a small Python library for creating simple webservers.\r\n\r\n## Installation\r\n\r\nTo install this library, use the pip command: `pip install bbwebservice`\r\n\r\n## Usage\r\n\r\n- how to import:\r\n\r\n```python\r\nfrom bbwebservice.webserver import * \r\n```\r\n\r\n1. Register pages for HTTP `GET`:\r\n   - `@register`: When using the `@register` decorator, you must define the named parameters `route` and `type`.\r\n     - `route`: The URL which must be requested via HTTP `GET` method for the decorated function to execute. The decorated function is expected to return the respective response content.\r\n     - `type`: The `type` parameter holds the intended MIME-type of the response.\r\n\r\n```python\r\n@register(route='/', type=MIME_TYPE.HTML)\r\ndef main_page():\r\n    return load_file('/content/index.html')\r\n```\r\n\r\n2. Register pages for HTTP `POST`:\r\n   - `@post_handler`: Works similarly to [1.], with the only difference being that it is mandatory for the decorated function to take a parameter.\r\n\r\n```python\r\n@post_handler(route='/makethread', type=MIME_TYPE.HTML)\r\ndef makethread(args):\r\n    return load_file('/content/post.html')\r\n```\r\n\r\n3. Redirect:\r\n  To redirect a request, you can return a `Redirect` object that specifies the path to which the request should be redirected.\r\n\r\n```python\r\n@register(route='/foo', type=MIME_TYPE.HTML)\r\ndef redirect():\r\n    return Redirect('/')\r\n```\r\n\r\n4. PartialContent and videostreaming:\r\n   To serve partial-content for video streaming or other applications, you can return a `PartialContent` object which takes the location of the streamed file and the chunk size which determines the size of the parts that should be streamed.\r\n\r\n```python\r\n@register(route='/video.mp4', type=MIME_TYPE.MP4)\r\ndef vid(args):\r\n    return PartialContent(\"/content/v.mp4\", 80000)\r\n```\r\n\r\n5. Error handler:\r\n   With the `@error_handler`, it is possible to provide default responses for requests with the specified error code.\r\n\r\n```python\r\n@error_handler(error_code=404, type=MIME_TYPE.HTML)\r\ndef not_found():\r\n    return load_file('/content/404.html')\r\n```\r\n\r\n6. Handler args:\r\n   Setting cookies, getting query strings, or setting custom headers are possible when you give your handler function a parameter.\r\n\r\n```python\r\n@post_handler(route='/', type=MIME_TYPE.HTML)\r\ndef login(args):\r\n    set_cookie(args, 'id', \"test\")\r\n    return load_file('/content/index.html')\r\n```\r\n\r\nThe server-supplied `args` value looks something like this, and changes to the provided value will be reflected in the server answer:\r\n\r\n```py\r\n{\r\n    \"query_string\": {},\r\n    \"flags\": [],\r\n    \"template_args\": {},\r\n    \"cookies\": {},\r\n    \"address\": (\"192.168.56.1\", 64361),\r\n    \"post\": bytearray(b\"\"),\r\n    \"request_header\": {\r\n        \"Host\": [\"192.168.56.1:5000\"],\r\n        \"Connection\": [\"keep-alive\"],\r\n        \"User-agent\": [\"Mozilla/5.0 (Windows NT 10.0\", \"Win64\", \"x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36\"],\r\n        \"Accept\": [\"text/html,application/xhtml+xml,application/xml\", \"q=0.9,image/avif,image/webp,image/apng,*/*\", \"q=0.8,application/signed-exchange\", \"v=b3\", \"q=0.7\"],\r\n        \"Accept-encoding\": [\"gzip, deflate\"],\r\n        \"Accept-language\": [\"de-DE,de\", \"q=0.9,en-US\", \"q=0.8,en\", \"q=0.7\"]\r\n    },\r\n    \"response\": \"<bbwebservice.http_parser.HTTP_Response object at 0x00000151D5718E50>\"\r\n}\r\n```\r\n\r\n7. Start the server (example of fully functional server):\r\n   To start the server, invoke the `start` function.\r\n\r\n```python\r\nfrom bbwebservice.webserver import *\r\n\r\n@register(route='/', type=MIME_TYPE.HTML)\r\ndef main():\r\n    return load_file('/content/index.html')\r\n\r\nstart()\r\n```\r\n\r\n8. With URL-templating you are able to match dynamic URLs like `/test/paul/1456379827256`\r\n\r\n```py\r\n@register(route= UrlTemplate('/test/{name:str}/{id:int}'), type=MIME_TYPE.TEXT)\r\ndef test(args):\r\n    return str(args[STORE_VARS.TEMPLATE_VARS])\r\n\r\n```\r\n\r\n## Server Configuration\r\n\r\nIn the directory `/config`, there is a file named `config.json`. Here you can configure the server:\r\n\r\n```json\r\n{\r\n    \"ip\": \"localhost\",\r\n    \"port\": 5000,\r\n    \"queue_size\": 10,\r\n    \"SSL\": false,\r\n    \"cert_path\" : \"\",\r\n    \"key_path\" : \"\"\r\n}\r\n```\r\n\r\nIf you intend to keep the application centrally (online), it is recommended to set the value of `ip` to either `default` or the IP address of the respective server. Additionally, it is advisable to activate `SSL` and set the corresponding paths for `cert_path` and `key_path`. \r\n\r\n### Recommended Ports\r\n\r\n- 5000: For testing purposes\r\n- 443: For HTTPS (SSL = true)\r\n- 80: For HTTP (SSL = false)\r\n\r\n## Logging\r\n\r\n```python\r\nlog_to_file()\r\nset_logging(LOGGING_OPTIONS.INFO, True)\r\nset_logging(LOGGING_OPTIONS.TIME, True)\r\nset_logging(LOGGING_OPTIONS.DEBUG, True)\r\nset_logging(LOGGING_OPTIONS.ERROR, True)\r\n```\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A bare bone webserver",
    "version": "1.0.5",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "48fe4a9fd0d42bcc284a4cc0620c813a82e2e3e267696ee53b14174c39d46654",
                "md5": "0cb9b22bdb1a45dab5b0652b6f74371c",
                "sha256": "6293727776d2e5fa4d2cadbfd4eb654f49a2bb4b645de02b313beb9540e9957e"
            },
            "downloads": -1,
            "filename": "bbwebservice-1.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0cb9b22bdb1a45dab5b0652b6f74371c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 20508,
            "upload_time": "2024-04-03T16:50:27",
            "upload_time_iso_8601": "2024-04-03T16:50:27.574646Z",
            "url": "https://files.pythonhosted.org/packages/48/fe/4a9fd0d42bcc284a4cc0620c813a82e2e3e267696ee53b14174c39d46654/bbwebservice-1.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3c86391ab079f24f5924240a5579304c874a8d1aedd59c88b32d1f374cfb23ab",
                "md5": "4771c37a08979a73034fc5a00a3fa097",
                "sha256": "ac44e528082ed36ff06269eb6e27447c625b604c2cd7a0101b8ef3ac106501b5"
            },
            "downloads": -1,
            "filename": "bbwebservice-1.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "4771c37a08979a73034fc5a00a3fa097",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 19730,
            "upload_time": "2024-04-03T16:50:28",
            "upload_time_iso_8601": "2024-04-03T16:50:28.784594Z",
            "url": "https://files.pythonhosted.org/packages/3c/86/391ab079f24f5924240a5579304c874a8d1aedd59c88b32d1f374cfb23ab/bbwebservice-1.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-03 16:50:28",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "bbwebservice"
}
        
Elapsed time: 0.22764s