Skip to main content

xy_stdio_request_base

说明

标准输入输出封装

源码仓库

安装

pip install xy_stdio_request_base

使用

python脚本
# main.py
import asyncio
from xy_stdio_request_base.Base import Base
from xy_string.utils import is_empty_string
from datetime import datetime

def printf(text):
    print(
        text,
        file=open(
            "./logs/server.log",
            "a+",
        ),
    )

class ServerBase(Base):
    input_text = None
    output_text = None
    error_text = None

    def on_output(self, output_text: str):
        self.output_text = output_text
        self.print_text(
            "output",
            self.output_text,
        )
        return super().on_output(self.output_text)

    def on_input(self, input_text: str):
        self.input_text = input_text
        self.print_text(
            "input",
            self.input_text,
        )
        return super().on_input(self.input_text)

    def on_error(self, error_text: str):
        self.error_text = error_text
        self.print_text(
            "error",
            self.error_text,
        )
        return super().on_error(self.error_text)

    def print_text(self, title: str, text: str):
        if not is_empty_string(text):
            printf(
                f"server post {datetime.now().isoformat()} {title} => {text} {self.identifier}",
            )

async def main():
    server = ServerBase()
    server.start()

    while not server.closed:
        server.write_input(
            f"server input => {datetime.now().isoformat()} {server.identifier}"
        )
        server.write_output(
            f"server output => {datetime.now().isoformat()} {server.identifier}"
        )
        server.write_error(
            f"server error => {datetime.now().isoformat()} {server.identifier}"
        )
        print_sep = server.input_text or server.output_text or server.error_text
        if not is_empty_string(server.input_text):
            printf(
                f"{datetime.now().isoformat()} input_text => {server.input_text}",
            )
            server.input_text = None
        if not is_empty_string(server.output_text):
            printf(
                f"{datetime.now().isoformat()} output_text => {server.output_text}",
            )
            server.output_text = None
        if not is_empty_string(server.error_text):
            printf(
                f"{datetime.now().isoformat()} error_text => {server.error_text}",
            )
            server.error_text = None
        if print_sep:
            printf(
                "=========================================================================",
            )
        await asyncio.sleep(3)

if __name__ == "__main__":
    asyncio.run(main())
python main.py

许可证

xy_stdio_request_base 根据 <木兰宽松许可证, 第2版> 获得许可。有关详细信息,请参阅 LICENSE 文件。

捐赠

如果小伙伴们觉得这些工具还不错的话,能否请咱喝一杯咖啡呢?

Pay-Total

联系方式

微信: yuyangiit
邮箱: yuyangit.0515@qq.com

Release files for xy-stdio-request-base 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xy-stdio-request-base 1.0.2
File Size Uploaded
xy_stdio_request_base-1.0.2.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xy-stdio-request-base 1.0.2
File Interpreter ABI Platform
xy_stdio_request_base-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 25.2 kB

Release files / xy_stdio_request_base-1.0.2.tar.gz

Download URL xy_stdio_request_base-1.0.2.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9a9d256905cb50df5cb26683924f2e42ebd362ee1d84ceb2313ddf22364d6cc2
BLAKE2b-256 checksum
How to use checksums
8ea3dbe87c08867dc922c2c7a6a61f39dde470708c84ed2c9b8225fe1205d175
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Release files / xy_stdio_request_base-1.0.2-py3-none-any.whl

Download URL xy_stdio_request_base-1.0.2-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9bbf5b84419f77d9e417bb1abc0f7266755662fa0477624ca4e414cc515caef1
BLAKE2b-256 checksum
How to use checksums
d1a03f7742a946799934a58029387e79dea684115538479dd53bbfbe40ae928d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page