zoukankan      html  css  js  c++  java
  • twisted

    Protocol 协议
    Factory 工厂
    reactor 反应器 (看作一个容器)
    ========= server.py =========

    from twisted.internet import reactor
    from twisted.internet.protocol import Factory,Protocol


    class Echo(Protocol):
    def connectionMade(self):
    self.transport.write('hi,welcome!')
    self.transport.loseConnection()

    def dataReceived(self, data):
    print(data)
    self.transport.write('haha...'+data)

    factory = Factory()
    factory.protocol = Echo

    reactor.listenTCP(8001,factory)
    reactor.run()

    ========= client.py =========
    from twisted.internet.protocol import Protocol,ClientFactory
    from twisted.internet import reactor


    class Echo(Protocol):
    def connectionMade(self):
    self.transport.write('hi, server')

    def dataReceived(self, data):
    print(data)


    class EchoClientFactory(ClientFactory):
    def startedConnecting(self, connector):
    print('connection starting ...')

    def buildProtocol(self, addr):
    print('addr')
    return Echo()

    def clientConnectionLost(self, connector, reason):
    print('lose reason',reason)

    def clientConnectionFailed(self, connector, reason):
    print('faild reason: ',reason)

    reactor.connectTCP('127.0.0.0',8001,EchoClientFactory())
    reactor.run()
  • 相关阅读:
    sql总结
    2018年6月10日笔记
    Docker入门之zabbix-agent篇
    2018年6月7日笔记
    2018年6月5日笔记
    Docker入门之container篇
    Docker入门之image篇
    Docker 入门
    2018年5月31日笔记
    2018年5月29日笔记
  • 原文地址:https://www.cnblogs.com/liyugeng/p/7989250.html
Copyright © 2011-2022 走看看