A light library to build tiny websites

Overview

WebNew - v0.1.1 - __init__.py

A light library to build tiny websites 一个用于搭建轻量级网站的库

对象:

(mappings,
WebApp,
Function,
DefaultSite)

1.mappings: 网站的所有站点储存,返回一个元组, 包含所有站点的定位符及其对应的类。(在v0.10版本 mappings为一个变量,故将其纳入对象行列

示例:
from webnew import *
site('Hello')  # 以GET访问 / 时,返回'Hello'
class greet: GET = lambda self: 'Hi, I\'m John.'  # 定义greet类的GET方法
newSite(greet, '/greet')   # 以GET访问 /greet 时,调用greet的GET方法,返回'Hi, I\'m John.'
print(mappings())  # 打印所有站点信息
输出:

('/', 'DefaultSite', '/greet', 'greet')

2.WebApp: 网站对应的App,是web.applaction对象。

示例:
from webnew import *
site('Hello')  # 以GET访问 / 时,返回'Hello'
reset()  # 更新WebApp的值
WebApp.run(globals())  # 运行WebApp
输出:

浏览 localhost:8080/ ,显示 Hello。

3.Function:函数类型,即 type(lambda: ...)

4.DefaultSite:网站的默认站点类,一般用于定义 /index ,建议通过调用 site() 方法设置。

方法:

1.site()

site( 
    _get: Union[object, Function] = lambda self: ...,
    _post: Union[object, Function] = lambda self: ...,
    mapping: str = '/'
) -> None

用法 :用于创建默认站点,一般用于定义网站的/index。 参数_get:当以GET访问时,调用的方法,值为一个函数,该函数需带一个 self 参数,若值为object,则在访问时直接返回该值。

_post:当以POST访问时,调用的方法,值为一个函数,该函数需带一个 self 参数,若值为object,则在访问时直接返回该值。

mapping:应为一个正则表达式,默认为'/',代表访问该站点时的接口,正则表达式所匹配的内容将会分组传入 _ge t和 _post 的参数。

示例1:
from webnew import *
site('Hello, GET.', 'Hello, POST.')  # 分别设置GET和POST的返回值
run(globals())  # 运行服务器
输出:

浏览或以GET访问 localhost:8080/ ,显示 Hello, GET. 。 以POSt访问 localhost:8080/ ,显示 Hello, POST. 。

示例2:
from webnew import *
site(lambda self, name: f'Hello, {name}.', mapping='/(.+)')  # 以GET访问/(.+)时,(.+)所匹配的内容会传入name参数再返回
run(globals())  # 运行服务器
输出:

浏览或以GET访问 localhost:8080/Tom ,显示 Hello, Tom. ,可以修改 /Tom 的值再次尝试 。

2.newSite()

newSite( 
    class_, 
    mapping: str = '/'
) -> None

用法 :用于创建新的站点,原理为调用传入类里的 GET 和 POST 方法。 参数class_:应为一个类,至少包含 GET 和 POST 方法中的一个用于处理GET和POST请求,至少需带一个 self 参数。

mapping:应为一个正则表达式,默认为'/',代表访问该站点时的接口,正则表达式所匹配的内容将会分组传入 class_ 的 GET 和 POST 的参数。

示例1:
from webnew import *
class index:  # 定义index类
    GET = lambda self: 'Hello, GET.'  # 当以GET访问时返回
    POST = lambda self: 'Hello, POST.'  # 当以POST访问时返回
newSite(index)  # 创建站点
run(globals())  # 运行服务器
输出:

浏览或以GET访问 localhost:8080/ ,显示 Hello, GET. 。 以POSt访问 localhost:8080/ ,显示 Hello, POST. 。

示例2:

请传入文件路径

') # 站点 / :返回提示HTML class open_file: # 站点类 def GET(self, path): # GET方法,path为匹配的文件地址 try: return open(path, encoding='utf-8') # 返回utf-8打开文件的内容 except Exception as e: return f'

{e}

' # 提示打开文件出错 newSite(open_file, '/(.+)') # 新建站点 /(.+) ,对应open_file类 run(globals()) # 运行服务器">
from webnew import *
site('
  

请传入文件路径

'
) # 站点 / :返回提示HTML class open_file: # 站点类 def GET(self, path): # GET方法,path为匹配的文件地址 try: return open(path, encoding='utf-8') # 返回utf-8打开文件的内容 except Exception as e: return f'

{e}

'
# 提示打开文件出错 newSite(open_file, '/(.+)') # 新建站点 /(.+) ,对应open_file类 run(globals()) # 运行服务器
输出:

浏览或以GET访问 localhost:8080/ ,显示 请传入文件路径 ,在路径后写入任意一个电脑文件路径,将会返回该文件的内容。可以尝试访问此链接访问Windows zh-CN 证书文件。

3.debug()

debug(
    mode: bool = True
) -> None

用法:用于设置网站是否调试,不调用debug()时默认为调试。 参数mode:应为布尔值,当为True时启用调试模式,为False时关闭,若调试模式启用,Python后端出现任何错误时会在此网页上报错。

示例:
from webnew import *
site(lambda self: error)  # 出现NameError
run(globals())  # 运行服务器
输出:

浏览或以GET访问 localhost:8080/ ,显示如下界面(界面大体一致,不同环境会有所差异):NameError.png

若在第二行后添加代码 debug(False)则显示 internal server error

4.reset()

reset() -> None

用法:用于重新加载 WebApp 对象的值。(WebApp可能会在未来的版本中替换为一个函数,reset()也可能会随之删除

示例:
from webnew import *
site()
print(WebApp.browser())  # 输出不固定
reset()
print(WebApp.browser())  # 输出不固定
输出:

由两次输出的数据不一致可知 reset() 改变了 WebApp 对象的值。

5.newSites()

newSites(
    *sites: tuple[Any, str]
) -> None

用法:用于一次新建多个站点。

参数

sites:形式为 ((class_, mapping), ...),意为循环执行 newSite(class_, mapping)

示例:
Go to page 1') # 定义 / 站点 class page_1: GET = lambda self: 'Go to page 2' # 定义page_1类 class page_2: GET = lambda self: 'Go to index' # 定义page_2类 newSites((page_1, '/page_1'), (page_2, '/page_2')) # 添加page_1和page_2站点 run(globals()) # 运行服务器">
from webnew import *
site('Go to page 1')  # 定义 / 站点
class page_1: GET = lambda self: 'Go to page 2'  # 定义page_1类
class page_2: GET = lambda self: 'Go to index'  # 定义page_2类
newSites((page_1, '/page_1'), (page_2, '/page_2'))  # 添加page_1和page_2站点
run(globals())  # 运行服务器
输出:

浏览或以GET访问 localhost:8080/ ,显示 Go to page 1 超链接,点击后跳转至 localhost:8080/page_1

显示 Go to page 2 超链接,点击后跳转至 localhost:8080/page_2, 显示 Go to index 超链接,点击后跳转至 localhost:8080/

6.run()

run(
    globals_ = None
) -> None

用法:用于运行服务器。

参数globals_:需要传入 globals()

说明:凡是调用 run() 都应传入 globals()

WebNew - v0.1.1 - request

# request 用于测试所编写的webnew网站程序

方法:

GET() / POST()

GET(
    ip: str = socket.gethostbyname(socket.gethostname()),
    localhost: int = 8080, mapping: str = '/'
) -> Optional[requests.Response]
POST(
    ip: str = socket.gethostbyname(socket.gethostname()),
    localhost: int = 8080, mapping: str = '/'
) -> Optional[requests.Response]

用法:通过 GET / POST 方法获取一个 requests.Response 对象

Owner
BT.Q
BT.Q
Ml-design-patterns - Source code accompanying O'Reilly book: Machine Learning Design Patterns

This is not an official Google product ml-design-patterns Source code accompanying O'Reilly book: Title: Machine Learning Design Patterns Authors: Val

Google Cloud Platform 1.5k Jan 05, 2023
IG Trading Algos and Scripts in Python

IG_Trading_Algo_Scripts_Python IG Trading Algos and Scripts in Python This project is a collection of my work over 2 years building IG Trading Algorit

191 Oct 11, 2022
An universal linux port of deezer, supporting both Flatpak and AppImage

Deezer for linux This repo is an UNOFFICIAL linux port of the official windows-only Deezer app. Being based on the windows app, it allows downloading

Aurélien Hamy 154 Jan 06, 2023
Packaging tools for shanty services.

parcel Packaging tools for shanty services. What? Services are docker containers deployed by shanty on a hosting appliance. Each service consists of t

0 Jan 20, 2022
An open letter in support of Richard Matthew Stallman being reinstated by the Free Software Foundation

An open letter in support of RMS. To sign, click here and name the file username.yaml (replace username with your name) with the following content

2.4k Jan 07, 2023
A fishing bot script written in Python!

A fishing bot script written in Python!

Anel Drocic 3 Nov 03, 2021
Doom o’clock is a website/project that features a countdown of “when will the earth end” and a greenhouse gas effect emission prediction that’s predicted

Doom o’clock is a website/project that features a countdown of “when will the earth end” and a greenhouse gas effect emission prediction that’s predicted

shironeko(Hazel) 4 Jan 01, 2022
Built as part of an assignment for S5 OOSE Subject CSE

Installation Steps: Download and install Python from here based on your operating system. I have used Python v3.8.10 for this. Clone the repository gi

Abhinav Rajesh 2 Sep 09, 2022
Sodium is a general purpose programming language which is instruction-oriented

Sodium is a general purpose programming language which is instruction-oriented (a new programming concept that we are developing and devising)

Satin Wuker 22 Jan 11, 2022
pythonOS: An operating system kernel made in python and assembly

pythonOS An operating system kernel made in python and assembly Wait what? It uses a custom compiler called snek that implements a part of python3.9 (

Abbix 69 Dec 23, 2022
Сервис служит прокси между cервисом регистрации ошибок платформы и системой сбора ошибок Sentry

Sentry Reg Service Сервис служит прокси между Cервисом регистрации ошибок платформы и системой сбора ошибок Sentry. Как развернуть Sentry onpremise. С

Ingvar Vilkman 13 May 24, 2022
C++ Environment InitiatorVisual Studio Code C / C++ Environment Initiator

Visual Studio Code C / C++ Environment Initiator Latest Version : v 1.0.1(2021/11/08) .exe link here About : Visual Studio Code에서 C/C++환경을 MinGW GCC/G

Junho Yoon 2 Dec 19, 2021
Animations made using manim-ce

ManimCE Animations Animations made using manim-ce The code turned out to be a bit complicated than expected.. It can be greatly simplified, will work

sparshg 17 Jan 06, 2023
A lightweight Python module to interact with the Mitre Att&ck Enterprise dataset.

enterpriseattack - Mitre's Enterprise Att&ck A lightweight Python module to interact with the Mitre Att&ck Enterprise dataset. Built to be used in pro

xakepnz 7 Jan 01, 2023
Impf Bot.py 🐍⚡ automation for the German

Impf Bot.py 🐍⚡ automation for the German "ImpfterminService - 116117"

251 Dec 13, 2022
Blender-3D-SH-Dma-plugin - Import and export Sonic Heroes Delta Morph animations (.anm) into Blender 3D

io_scene_sonic_heroes_dma This plugin for Blender 3D allows you to import and ex

Psycrow 3 Mar 22, 2022
Spinning waffle from waffle shaped python code

waffle Spinning waffle from waffle shaped python code Based on a parametric curve: r(t) = 2 - 2*sin(t) + (sin(t)*abs(cos(t)))/(sin(t) + 1.4) projected

Viljar Femoen 5 Feb 14, 2022
This python code will get requests from SET (The Stock Exchange of Thailand) a previously-close stock price and return it in Thai Baht currency using beautiful soup 4 HTML scrapper.

This python code will get requests from SET (The Stock Exchange of Thailand) a previously-close stock price and return it in Thai Baht currency using beautiful soup 4 HTML scrapper.

Andre 1 Oct 24, 2022
Today I Commit (1일 1커밋) 챌린지 알림 봇

Today I Commit Challenge 1일1커밋 챌린지를 위한 알림 봇 config.py github_token = "github private access key" slack_token = "slack authorization token" channel = "

sunho 4 Nov 08, 2021
A tool for RaceRoom Racing Experience which shows you launch data

R3E Launch Tool A tool for RaceRoom Racing Experience which shows you launch data. Usage Run the tool, change the Stop Speed to whatever you want, and

Yuval Rosen 2 Feb 01, 2022