当前位置: 首页 > news >正文

Twisted study notes[2]

文章目录

  • permanent data
  • references.

permanent data

  1. the persistent data can be saved in the subclass of Factory .
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactorclass MyProtocol(Protocol):def __init__(self, factory):self.factory = factorydef connectionMade(self,factory):# Called when a new client connectsself.factory.numProtocols=self.factory.numProtocols+1client_ip = self.transport.getPeer().hostprint(f"New connection from: {client_ip}")self.transport.write(b"Welcome! Type something...\r\n")def dataReceived(self, data):# Called when data is received from the clientprint(f"Received: {data.decode().strip()}")self.transport.write(b"Echo: " + data)def connectionLost(self, reason):# Called when the connection is closedself.factory.numProtocols=self.factory.numProtocols-1print(f"Client disconnected. Reason: {reason.getErrorMessage()}")class MyFactory(Factory):numProtocols=0def buildProtocol(self, addr):return MyProtocol(self)# Start the server on port 8000
reactor.listenTCP(8000, MyFactory())
print("Server running on port 8000...")
reactor.run()
  1. numProtocols survives in the instance of MyFactory .

  2. The buildProtocol method of the Factory while it meet every comming connection.

  3. The connectionLost function will be callied when any connection-specific objects was disconnect.

  4. to call loseConnection without worrying about transport writes being lost ,when you need to close a connection.loseConnection() is the preferred method - it performs a clean shutdown by:

    • Writing any pending data

    • Closing the connection only after all data is sent

    • Properly terminating the connection

from twisted.internet import protocolclass MyProtocol(protocol.Protocol):def connectionMade(self):print("Connection made")def loseConnection(self):# This is the proper way to close the connectionself.transport.loseConnection()def connectionLost(self, reason):print("Connection lost:", reason)

For immediate termination (not recommended normally), use abortConnection():

transport.abortConnection()
  1. TCP4ServerEndpoint Implements TCP server endpoint with an IPv4 configuration
endpoint = TCP4ServerEndpoint(reactor, 8007)
endpoint.listen(QOTDFactory())
reactor.run()

reactor.run() launch the reactor,waits forever for connections to arrive on the port.through reactor.stop() ,you can stop the reactor .

references.

  1. https://docs.twisted.org/
  2. deepseek
http://www.dtcms.com/a/288984.html

相关文章:

  • Linux——进程的退出、等待与替换
  • ThinkSound:阿里开源首个“会思考”的音频生成模型——从“看图配音”到“听懂画面”的技术跃迁
  • C++ Primer(第5版)- Chapter 7. Classes -004
  • Dockerfile配置基于 Python 的 Web 应用镜像
  • 考研最高效的准备工作是什么
  • docker制作前端镜像
  • JVM-Java
  • 每日算法刷题Day50:7.20:leetcode 栈8道题,用时2h30min
  • 全面解析 JDK 提供的 JVM 诊断与故障处理工具
  • 零基础学习性能测试第二章-JVM如何监控
  • Android系统5层架构
  • 【论文笔记】OccluGaussian解决大场景重建中的区域遮挡问题
  • 5G NR PDCCH之信道编码
  • c#:管理TCP服务端发送数据为非16进制
  • 4、ubuntu | dify创建知识库 | 上市公司个股研报知识库
  • Python知识点4-嵌套循环break和continue使用死循环
  • 统计与大数据分析和数字经济:专业选择指南
  • LP-MSPM0G3507学习--07定时器之二定时节拍
  • 使用“桥接模式“,实现跨平台绘图或多类型消息发送机制
  • SpringBoot的介绍和项目搭建
  • 【C语言】字符串与字符函数详解(上)
  • C++ 详谈继承体系下的构造函数和析构函数
  • k8s:离线添加集群节点的相关组件安装与升级
  • GeoServer 信息泄漏漏洞复现(CVE-2025-27505)
  • 周志华《机器学习导论》第11章 特征选择与稀疏学习
  • 机器学习-数据预处理
  • 闲庭信步使用图像验证平台加速FPGA的开发:第二十六课——正弦波DDS的FPGA实现
  • leetcode75【经典动态规划】之:最长公共子序列
  • nginx源码解读-------整体架构
  • 30天打牢数模基础-LightGBM讲解