Coder Social home page Coder Social logo

ports's Introduction

PORTS

A scalable, customizable, configurable, programmable, webserver framework

Installation

Simply install python and type pip install ports.py

Usage

Start a static server in three lines:

from ports import APP, static_APP
app = static_APP()
app.run()

then all files in www/ will be hosted

Start a dynamic server:

from ports import APP, static_APP


app = APP()

@app.route("/")
def index():
  return "hi!"
@app.route("/hi/") # unlike flask you can put either /hi or /hi/ and it will work fine
def hi():
  return "hello"
@app.route("/bye") # will still work
def bye():
  return "bye"

app.run()

Make your own handler/framework:

from ports import APP, static_APP, tools


app = APP()
app.on("request")
def frame(app,req):
  if req.method == "GET":
    if req.path == "/":
      return "Index (/)"
app.run()

Make a response:

from ports import APP, static_APP, tools, Response


app = APP()
app.route("/")
def index():
  resp = Response(status="200 OK", content="hello world thru resp")
  resp.headers.add("key","value")
  return resp
app.run()

Events:

from ports import APP, static_APP, tools, Response


app = APP()
app.route("/")
def index():
  resp = Response(status="200 OK", content="hello world thru resp")
  resp.headers.add("key","value")
  return resp
@app.on("bind")
def binded(sock):
  print("connected")
# all events can be accessed through app.all_events
app.run()

Template rendering:

from ports import APP, static_APP, tools


app = APP()

@app.route("/")
def index():
  return tools.render_template("index.html")

app.run("0.0.0.0",8080)

Renders templates/index.html ^

Arguments:

from ports import APP, static_APP


app = APP()

@app.route("/")
def index(req):
  if req.args == {}:
    return "No args"
  if "name" not in req.args:
    return "please give arg 'name'"
  name = req.args["name"]
  return f"hello {name}"


app.run("0.0.0.0",8080)

Cookies:

from ports import APP, static_APP



app = APP()

@app.route("/")
def index():
  cookiejar = app.Cookies.get_all() # return in json name:value
  value = app.Cookies.get("pwd")
  if "username" in cookiejar:
    app.Cookies.delete("username")
  else:
    app.Cookies.set("username","example")

app.run("0.0.0.0",8080)

Send file:

from ports import APP, static_APP
from ports import tools


app = APP()
@app.route("/")
def index():
  return "Favicon Activated"
@app.route("/favicon.ico")
def favicon():
  return tools.send_file("favicon.ico")
app.run("0.0.0.0",8080)

Extensions: "app.py"

import ports
from ports import managers

app = ports.APP(__name__)
ext = managers.extensions
ext.register("extension.py")
# to register all "python" files in a directory:
#ext.regdir("extensions/")


@app.route("/exts/")
def exts():
  return "try route /"


app.run()

"extension.py"

import ports
from ports import managers



class extension:
  def __init__(self,app):
    self.name = "name of extension"
    self.app = app
  def run(self):
    @self.app.route("/")
    def index():
      return "hi!"
    @self.app.route("/hello/")
    def hello():
      return str(self.app.config)

def setup(app):
  ext = extension(app)
  ext.run()

the object app is given to the function setup similarly it can also be used as:

import ports
from ports import managers

def extension(app):
  @app.route("/")
  def index():
    return "hi!"
  @app.route("/hello/")
  def hello():
    return str(app.config)

def setup(app):
  extension(app)

ports's People

Contributors

wynwxst avatar

Stargazers

 avatar

Watchers

 avatar

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.