Giter Site home page Giter Site logo

sbeaver's Introduction

Sbeaver

This is sbeaver - a lightweight and extremely simple http server for creating APIs.

  • add auto-restart on change code
  • fix cookies
  • rewrite on sockets

To install sbeaver on your system, you can use the pip install sbeaver command, or you can download this repository and run python setup.py install

Usage

import sbeaver

server = sbeaver.Server(address="localhost", port=8000, sync=True)

this code will import and make the basic configuration of the sbeaver server in your project To start, you need to call server.start()

Decorators are used to bind paths on the server to internal methods.

sbind is used to bind static paths (e.g. home page)

@server.sbind('/')
def index(request):
    return 200, {'status':'ok'}

image

You can also bind a regular expression using bind

@server.bind(r'/regex/(\w*)(?:\.|/)(\w*)(?:|/)')
def regex(request, param1 = None, param2 = None):
    return 200, {'first':param1, 'second':param2}

image

Or if you can't work with the regex, you can use easy bind(ebind)

@server.ebind('/ebind/<submethod>/<method>')
def method(request,  submethod = None, method = None):
    return 200, {'section':submethod, 'method':method}

image

You can also interact with the user's request. For example, this code will return all known information about a particular request

@server.sbind('/info')
def info(request):
    request.parse_all() # get and save data, url params, ip from request
    return 200, {'info':request.dict}

image

If the function required for the path is not found during request processing, the code404 function is called. It can be assigned by code

@server.code404()
def page_not_found(request):
    return {'error404': f"path {request.path} not found"}

image

Also done with 500 code. When an exception occurs during the processing of a user request, an error will be displayed and the code function will be called

@server.code500()
def internal_server_error(request):
    return {'error500': f"Exception happened"}

Redirecting

You can redirect user to another page using method redirect

Example:

@server.sbind('/') # static bind
def args(request):
    return sbeaver.redirect(307,'/info') # redirect with data(307 code)

Files

You can return files using method file

@server.sbind('/photo')
def photo(request):
    return sbeaver.open_file('beaver.png', sbeaver.Types.Image.png)

sbeaver's People

Contributors

guhbap avatar santaspeen avatar

Stargazers

 avatar

Forkers

santaspeen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.