socketify.py/README.md

220 wiersze
10 KiB
Markdown
Czysty Zwykły widok Historia

2022-05-28 13:47:26 +00:00
# socketify.py
2022-11-05 15:58:00 +00:00
2022-05-24 19:37:16 +00:00
2022-11-08 11:25:54 +00:00
<p align="center">
2022-11-08 11:26:40 +00:00
<a href="https://github.com/cirospaciari/socketify.py"><img src="https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/logo.png" alt="Logo" height=170></a>
2022-11-08 11:25:54 +00:00
<br />
<br />
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/linux.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/linux.yml/badge.svg" /></a>
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/windows.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/windows.yml/badge.svg" /></a>
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos.yml/badge.svg" /></a>
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml/badge.svg" /></a>
<br/>
2022-11-26 11:31:19 +00:00
<a href='https://github.com/cirospaciari/socketify.py'><img alt='GitHub Clones' src='https://img.shields.io/badge/dynamic/json?color=success&label=Clones&query=count&url=https://gist.githubusercontent.com/cirospaciari/2243d59951f4abe4fd2000f1e20bc561/raw/clone.json&logo=github'></a>
2022-11-09 08:48:04 +00:00
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
2022-11-26 11:31:19 +00:00
2022-11-08 11:25:54 +00:00
</p>
2022-05-24 19:37:16 +00:00
2022-10-11 12:33:55 +00:00
2023-01-09 15:03:46 +00:00
## :bookmark_tabs: Documentation
See the full docs in [docs.socketify.dev](https://docs.socketify.dev) or in [/docs/README.md](docs/README.md)
2022-11-08 11:25:54 +00:00
2023-01-09 15:03:46 +00:00
Also take a look in the examples in [/examples](https://github.com/cirospaciari/socketify.py/tree/main/examples)
2022-11-11 22:50:12 +00:00
## 💡 Features
2022-11-08 11:25:54 +00:00
- WebSocket with pub/sub support
- Fast and reliable Http/Https
2022-11-08 11:25:54 +00:00
- Support for Windows, Linux and macOS Silicon & x64
- Support for [`PyPy3`](https://www.pypy.org/) and [`CPython`](https://github.com/python/cpython)
2022-11-12 19:47:50 +00:00
- Dynamic URL Routing with Wildcard & Parameter support
- Sync and Async Function Support
2022-11-12 19:48:18 +00:00
- Really Simple API
2022-11-12 19:47:50 +00:00
- Fast and Encrypted TLS 1.3 quicker than most alternative servers can do even unencrypted, cleartext messaging
- Per-SNI HttpRouter Support
- Proxy Protocol v2
- Shared or Dedicated Compression Support
- Max Backpressure, Max Timeout, Max Payload and Idle Timeout Support
- Automatic Ping / Pong Support
- Per Socket Data
2023-01-06 19:11:19 +00:00
- [`Middlewares`](https://docs.socketify.dev/middlewares.html)
- [`Templates`](https://docs.socketify.dev/templates.html) Support (examples with [`Mako`](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_mako.py) and [`Jinja2`](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_jinja2.py))
- [`ASGI Server`](https://docs.socketify.dev/cli.html)
- [`WSGI Server`](https://docs.socketify.dev/cli.html)
- [`Plugins/Extensions`](https://docs.socketify.dev/extensions.html)
2022-12-01 21:55:19 +00:00
2022-11-11 22:50:12 +00:00
## :mag_right: Upcoming Features
2022-12-01 21:55:19 +00:00
- In-Memory Cache Tools
2022-11-08 14:06:18 +00:00
- Fetch like API powered by libuv
- Async file IO powered by libuv
- Full asyncio integration with libuv
2022-12-02 00:10:19 +00:00
- SSGI Server spec and support
- RSGI Server support
2022-11-08 14:06:18 +00:00
- Full Http3 support
2022-11-08 14:15:37 +00:00
- [`HPy`](https://hpyproject.org/) integration to better support [`CPython`](https://github.com/python/cpython), [`PyPy`](https://www.pypy.org/) and [`GraalPython`](https://github.com/oracle/graalpython)
2022-11-12 19:47:50 +00:00
- Hot Reloading
2022-11-08 11:25:54 +00:00
2022-11-12 22:57:23 +00:00
We created and adapted the full C API from [uNetworking/uWebSockets](https://github.com/uNetworking/uWebSockets) and will integrate libuv powered fetch and file IO, this same C API is used by [Bun](https://bun.sh/)
2022-11-08 11:25:54 +00:00
2022-11-17 17:01:19 +00:00
Join Github [`Discussions`](https://github.com/cirospaciari/socketify.py/discussions) or [`Discord`](https://discord.socketify.dev/) for help and have a look at the development progress.
2022-11-08 14:36:04 +00:00
2022-11-11 22:50:12 +00:00
## :zap: Benchmarks
2022-12-20 10:31:46 +00:00
Socketify WebFramework HTTP requests per second (Linux x64)
2022-11-08 13:07:18 +00:00
2022-12-20 10:31:46 +00:00
![image](https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/bench-bar-graph-general.png)
2022-11-08 13:19:34 +00:00
2022-12-20 10:31:46 +00:00
WSGI Server requests per second (Linux x64)
![image](https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/bench-bar-graph-wsgi.png)
ASGI Server requests per second (Linux x64)
2022-11-11 15:27:21 +00:00
2022-12-20 10:31:46 +00:00
![image](https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/bench-bar-graph-asgi.png)
WebSocket messages per second (Linux x64)
2022-11-11 15:27:21 +00:00
2022-12-20 10:37:37 +00:00
![image](https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/bench-bar-graph-websockets.png)
2022-11-11 15:27:21 +00:00
2022-12-20 10:31:46 +00:00
Http tested with TFB tool plaintext benchmark<br/>
2022-11-11 15:27:21 +00:00
WebSocket tested with [Bun.sh](https://bun.sh) bench chat-client <br/>
2023-01-11 09:57:33 +00:00
Source code in [TechEmPower](https://github.com/TechEmpower/FrameworkBenchmarks) and for websockets in [bench](https://github.com/cirospaciari/socketify.py/tree/main/bench)<br/>
2022-11-11 15:27:21 +00:00
2022-11-10 11:44:36 +00:00
Machine OS: Debian GNU/Linux bookworm/sid x86_64 Kernel: 6.0.0-2-amd64 CPU: Intel i7-7700HQ (8) @ 3.800GHz Memory: 32066MiB
2022-11-10 11:42:24 +00:00
2022-11-11 22:52:13 +00:00
## 📦 Installation
2022-11-08 11:34:26 +00:00
For macOS x64 & Silicon, Linux x64, Windows
2022-05-24 19:37:16 +00:00
```bash
2023-01-03 21:40:04 +00:00
pip install socketify
2022-05-28 20:04:35 +00:00
#or specify PyPy3
2023-01-03 21:40:04 +00:00
pypy3 -m pip install socketify
2022-05-29 00:00:21 +00:00
#or in editable mode
2023-01-03 21:40:04 +00:00
pypy3 -m pip install -e socketify
2022-05-24 19:37:16 +00:00
```
2022-11-08 11:34:26 +00:00
Using install via requirements.txt
2022-05-29 00:00:21 +00:00
```text
2023-01-03 21:40:04 +00:00
socketify
2022-05-29 00:00:21 +00:00
```
2022-05-24 19:37:16 +00:00
```bash
2022-05-29 00:00:21 +00:00
pip install -r ./requirements.txt
#or specify PyPy3
pypy3 -m pip install -r ./requirements.txt
2022-05-24 19:37:16 +00:00
```
2022-05-25 14:01:56 +00:00
2022-11-13 21:24:12 +00:00
If you are using linux or macOS, you may need to install libuv and zlib in your system
macOS
```bash
brew install libuv
brew install zlib
```
Linux
```bash
apt install libuv1 zlib1g
```
2022-11-11 22:52:13 +00:00
## 🤔 Usage
2022-11-08 11:34:26 +00:00
Hello world app
```python
from socketify import App
app = App()
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()
```
2022-11-08 11:25:54 +00:00
SSL version sample
2022-05-25 14:01:56 +00:00
``` python
2022-05-28 13:47:26 +00:00
from socketify import App, AppOptions
2022-05-25 14:01:56 +00:00
app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234"))
2022-05-28 13:47:26 +00:00
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
2022-05-28 19:59:08 +00:00
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
2022-05-25 14:01:56 +00:00
app.run()
2022-05-28 20:04:35 +00:00
```
2022-11-08 15:09:57 +00:00
WebSockets
```python
from socketify import App, AppOptions, OpCode, CompressOptions
def ws_open(ws):
print('A WebSocket got connected!')
ws.send("Hello World!", OpCode.TEXT)
def ws_message(ws, message, opcode):
#Ok is false if backpressure was built up, wait for drain
ok = ws.send(message, opcode)
app = App()
app.ws("/*", {
'compression': CompressOptions.SHARED_COMPRESSOR,
'max_payload_length': 16 * 1024 * 1024,
'idle_timeout': 12,
'open': ws_open,
'message': ws_message,
2023-01-05 12:01:05 +00:00
'drain': lambda ws: print(f'WebSocket backpressure: {ws.get_buffered_amount()}'),
'close': lambda ws, code, message: print('WebSocket closed'),
'subscription': lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
2022-11-08 15:09:57 +00:00
})
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
app.run()
```
2022-11-08 11:34:26 +00:00
We have more than 20 examples [click here](https://github.com/cirospaciari/socketify.py/tree/main/examples) for more
2022-11-11 22:52:13 +00:00
## :hammer: Building from source
2022-05-28 20:04:35 +00:00
```bash
#clone and update submodules
git clone https://github.com/cirospaciari/socketify.py.git
cd ./socketify.py
git submodule update --init --recursive --remote
#you can use make linux, make macos or call Make.bat from Visual Studio Development Prompt to build
cd ./src/socketify/native/ && make linux && cd ../../../
2022-05-29 00:00:21 +00:00
#install local pip
2022-11-04 23:08:20 +00:00
pypy3 -m pip install .
2022-05-29 00:00:21 +00:00
#install in editable mode
pypy3 -m pip install -e .
2022-05-28 20:04:35 +00:00
#if you want to remove
pypy3 -m pip uninstall socketify
2022-10-11 12:33:55 +00:00
```
2022-11-08 14:25:29 +00:00
2022-11-11 22:50:12 +00:00
## :briefcase: Commercially supported
2022-11-08 14:29:53 +00:00
I'm a Brazilian consulting & contracting company dealing with anything related with [socketify.py](https://github.com/cirospaciari/socketify.py) and [socketify.rb](https://github.com/cirospaciari/socketify.rb)
2022-11-08 14:25:29 +00:00
2022-11-08 14:31:56 +00:00
Don't hesitate sending a mail if you are in need of advice, support, or having other business inquiries in mind. We'll figure out what's best for both parties.
2022-11-08 14:25:29 +00:00
Special thank's to [uNetworking AB](https://github.com/uNetworking) to develop [uWebSockets](https://github.com/uNetworking/uWebSockets), [uSockets](https://github.com/uNetworking/uSockets) and allow us to bring this features and performance to Python and PyPy
2022-11-09 08:48:04 +00:00
2022-11-11 22:50:12 +00:00
## :heart: Sponsors
2023-01-10 20:46:39 +00:00
If you like to see this project thrive, you can sponsor us on GitHub too. We need all the help we can get.
Thank you [`Otavio Augusto`](https://github.com/middlebaws) to be the first sponsor of this project!
2022-11-09 08:48:04 +00:00
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
2022-11-11 22:22:48 +00:00
2022-11-11 22:50:12 +00:00
## :star: Stargazers
2022-11-11 22:22:48 +00:00
[![Stargazers repo roster for @cirospaciari/socketify.py](https://reporoster.com/stars/dark/cirospaciari/socketify.py)](https://github.com/cirospaciari/socketify.py/stargazers)
2022-11-11 22:50:12 +00:00
## :wrench: Forkers
2022-11-11 22:22:48 +00:00
[![Forkers repo roster for @cirospaciari/socketify.py](https://reporoster.com/forks/dark/cirospaciari/socketify.py)](https://github.com/cirospaciari/socketify.py/network/members)
2022-11-11 22:50:12 +00:00
## :question: socketify.py vs japronto
People really want to compare with japronto, but this projects are not really comparable. Socketify is an active project and will be maintained over time with security updates and new features, japronto don't get any github updates since 2020 and don't get any src update since 2018, japronto don't support SSL, WebSockets, [`PyPy3`](https://www.pypy.org/), Windows or macOS Silicon, socketify will support Http3 and a lot more features.
And yes, we can be faster than japronto when all our features and goals are achieved, and we are probably faster than any current maintained solution out there.
## :grey_question: uvloop
We don't use uvloop, because uvloop don't support Windows and PyPy3 at this moment, this can change in the future, but right now we want to implement our own libuv + asyncio solution, and a lot more.
## :dizzy: CFFI vs Cython vs HPy
Cython performs really well on Python3 but really bad on PyPy3, CFFI are chosen for better support PyPy3 until we got our hands on an stable [`HPy`](https://hpyproject.org/) integration.
2022-11-17 22:41:59 +00:00