Skip to main content

xy_stdio_request_base 模块

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xy_stdio_request_base-1.0.2.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

xy_stdio_request_base-1.0.2-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file xy_stdio_request_base-1.0.2.tar.gz.

File metadata

  • Download URL: xy_stdio_request_base-1.0.2.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for xy_stdio_request_base-1.0.2.tar.gz
Algorithm Hash digest
SHA256 9a9d256905cb50df5cb26683924f2e42ebd362ee1d84ceb2313ddf22364d6cc2
MD5 dc77a0473e3ff440595f1bdef639fe39
BLAKE2b-256 8ea3dbe87c08867dc922c2c7a6a61f39dde470708c84ed2c9b8225fe1205d175

See more details on using hashes here.

File details

Details for the file xy_stdio_request_base-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for xy_stdio_request_base-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9bbf5b84419f77d9e417bb1abc0f7266755662fa0477624ca4e414cc515caef1
MD5 6574e1040bffdb486030b105c3c4e608
BLAKE2b-256 d1a03f7742a946799934a58029387e79dea684115538479dd53bbfbe40ae928d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page