当前位置: 首页 > 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.lryc.cn/news/593892.html

相关文章:

  • 六年级数学知识边界总结思考-下册
  • 在Ubutu22系统上面离线安装Go语言环境【教程】
  • 传染病监测(六):随机模型 —— 为什么小规模疫情像掷骰子?
  • 【LeetCode 热题 100】200. 岛屿数量——DFS
  • MCP实战案例|Trae2.0 一键创建旅行助手并一键部署EdgeOne
  • axios二次封装-单个、特定的实例的拦截器、所有实例的拦截器。
  • Laravel 原子锁概念讲解
  • sqli-labs靶场通关笔记:第34-37关 宽字节注入的其他情况
  • docker Neo4j
  • PDF 编辑器:多文件合并 拆分 旋转 顺序随便调 加水印 密码锁 页码背景
  • Python 进阶知识之numpy库(一)
  • 考研最高效的准备工作是什么
  • 【JDK内置工具】常用工具和实战指令
  • 30天打牢数模基础-决策树讲解
  • Docker在NAS部署MoonTV+OrionTV
  • [Python] -项目实战8- 构建一个简单的 Todo List Web 应用(Flask)
  • 深度学习×第10卷:她用一块小滤镜,在图像中找到你
  • 嵌入式硬件篇---按键
  • 嵌入式硬件篇---继电器
  • USB 2.0 vs USB 3.0:全面技术对比与选择指南
  • 2025《艾诺提亚失落之歌》新手攻略
  • 基于单片机出租车计价器设计
  • DMA控制器(Direct Memory Access Controller)是什么?
  • 用户端功能清单设计指南:从核心模块到优先级排序
  • 面试150 添加与搜索单词--数据结构设计
  • 前端的测试
  • 详解Mysql索引合并
  • 二、Spark 开发环境搭建 IDEA + Maven 及 WordCount 案例实战
  • 每日一题7.20
  • Spring之事务使用指南