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

相关文章:

  • Node.js worker_threads 性能提升
  • ARM 学习笔记(三)
  • C 语言经典编程题实战:从基础算法到趣味问题全解析
  • python学智能算法(二十六)|SVM-拉格朗日函数构造
  • Beamer-LaTeX学习(教程批注版)【6】
  • AtCoder Beginner Contest 415
  • Linux系统中全名、用户名、主机名的区别
  • Unity学习笔记(五)——3DRPG游戏(2)
  • 《拆解WebRTC:NAT穿透的探测逻辑与中继方案》
  • (苍穹外卖)暑假学习理解P2
  • 平安车管家|中国平安车管家入职测评16PF瑞文IQ测评答题攻略及真题题库
  • UDP中的单播,多播,广播(代码实现)
  • securecrt连接服务器报错 Key exchange failed 怎么办
  • 在服务器无网络的环境下安装 VS Code Remote-SSH 组件
  • Linux-基础知识总结
  • 【算法300题】:双指针
  • 搭建大模型
  • Dockerfile配置基于 Python 的 Web 应用镜像
  • 前端静态资源免费cdn服务推荐
  • 【分布式 ID】详解百度 uid-generator(源码篇)
  • 企业安全防护:堡垒机技术解析
  • WireShark抓包分析TCP数据传输过程与内容详解
  • Linux场景常见的几种安装方式
  • 在C++里如何避免栈内存溢出
  • C++ primer知识点总结
  • 深度学习图像分类数据集—八种贝类海鲜食物分类
  • 基于Chinese-LLaMA-Alpaca-3的多模态中医舌诊辅助诊断系统设计与实现
  • day24——Java高级技术深度解析:单元测试、反射、注解与动态代理
  • 零基础 “入坑” Java--- 十三、再谈类和接口
  • ABP VNext + Playwright E2E:前后端一体化自动化测试