V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yang2yang
V2EX  ›  Python

求解一个 Twisted 简单客户端问题(新手勿喷)

  •  
  •   yang2yang ·
    yang2yang · 2015-12-18 19:58:52 +08:00 · 2717 次点击
    这是一个创建于 3053 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from twisted.internet import reactor, protocol
    
    class EchoClient(protocol.Protocol):
    
        def connectionMade(self):
            while(True):
                self.transport.write("111")
    
        def dataReceived(self, data):
            print "Server said:",data
            # self.transport.loseConnection()
    
        def connectionLost(self, reason):
            print "connection lost"
    

    这里我想用 Twisted 做一个简单的客户端,用来不断地发送同一个字符串,但是这里不知道为什么无法发送成功?
    按我的理解这里self.transport.write("111")应该已经发送给了服务器了吧,但是经过测试并不是这样?求大牛解惑。新手问的问题比较简单,不要见怪。

    7 条回复    2015-12-21 21:19:28 +08:00
    fangdingjun
        1
    fangdingjun  
       2015-12-19 10:05:49 +08:00
    你那个 while 阻塞住整个程序了, 你没有明白 twisted 的核心思想
    self.transport.write 在控制权回到 reactor 之后才会发送出去
    你的这种写法,控制权永远无法回到 reactor 中

    你可以试试这样

    ```python

    from twisted.internet import reactor, protocol

    class EchoClient(protocol.Protocol):

    def connectionMade(self):
    reactor.callLater(0, self.send_data, "1111")

    def send_data(self, data):
    self.transport.write(data)
    reactor.callLater(0, self.send_data, "1111")

    def dataReceived(self, data):
    print "Server said:",data
    # self.transport.loseConnection()

    def connectionLost(self, reason):
    print "connection lost"

    ```
    fangdingjun
        2
    fangdingjun  
       2015-12-19 10:10:05 +08:00
    上面代码,缩进被吃了,在 gist 上查看
    https://gist.github.com/fangdingjun/0e320ac704550403b04b
    yang2yang
        3
    yang2yang  
    OP
       2015-12-19 12:31:18 +08:00
    @fangdingjun 真的很感谢,还以为这个帖子沉了呢- -,刚才试了一下基本没什么问题了,但是我的服务器将传送过来的数据打印出来,有时出现了这种情况
    1111
    1111
    1111
    11111111
    这样的样子,请问这是怎么回事?我这里只开了一个客户端,一个服务器。是因为 Twisted 异步问题导致的吗?还有为什么控制权要回到 reacotr 中数据才能发送出去?
    tangshiping
        4
    tangshiping  
       2015-12-19 20:16:04 +08:00
    可以在 dataReceived 中往服务端发送数据,这样每次收到从服务端发来的数据,会再次向服务端发送数据
    yang2yang
        5
    yang2yang  
    OP
       2015-12-19 22:06:30 +08:00
    @tangshiping 我明白你的意思,但是这样我想实现的并不是这样的结果,我不想通过服务器影响客户端,要做一个一直发送数据的客户端
    fangdingjun
        6
    fangdingjun  
       2015-12-21 09:11:34 +08:00
    https://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith

    tcp 发送消息,发送的消息被拆分或被合并都是正常情况

    为什么控制权要回到 reacotr 中数据才能发送出去, 这个问题你真的要仔细看 twisted 的文档
    yang2yang
        7
    yang2yang  
    OP
       2015-12-21 21:19:28 +08:00
    @fangdingjun 谢谢,又对网络编程有了一些了解
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5473 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:49 · PVG 15:49 · LAX 00:49 · JFK 03:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.