Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38912

Re: Java NIO server and Python asyncore client

Newsgroups comp.lang.python
Date 2013-02-15 01:41 -0800
References <ce1ca918-2bbe-440b-95eb-89dc257ffc9f@googlegroups.com>
Message-ID <8f766ae3-1119-4c2c-a851-9fa0eb6e5472@googlegroups.com> (permalink)
Subject Re: Java NIO server and Python asyncore client
From Petri Heinilä <hevi00@gmail.com>

Show all headers | View raw


On Tuesday, February 5, 2013 10:09:28 AM UTC+2, foobar...@gmail.com wrote:
> Can someone help answer this?
> 
> http://stackoverflow.com/questions/14698020/java-nio-server-and-python-asyncore-client
> 
> 
> 
> Blocking python client works, asyncore doesn't work.
> 

There was return missing in writeable().

Modified code::

----
import socket
import select
import asyncore

class Connector(asyncore.dispatcher):
    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.debug = True
        self.buffer = bytes("hi","ascii")
        self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
        print("Connector.connect(({},{}))".format(host,port))
        self.connect((host, port))

    def handle_connect(self):
        print("handle_connect()") # not called <------------------

    def handle_read(self):
        print("handle_read()")
        self.recv(4096)
        self.close()

    def writable(self):
        print("writable()")
        return len(self.buffer) > 0 # remember RETURN

    def handle_write(self):
        print("handle_write()")
        sent = self.send(self.buffer)
        print("send({})".format(self.buffer[0:sent]))
        self.buffer = self.buffer[sent:]

    def handle_close(self):
        print("handle_close()")
        self.close()

connector = Connector("localhost", 12000) #  Handler()
print("asyncore.loop() enter")
asyncore.loop() 
print("asyncore.loop() leave")
----

BSD socket communication framework does not itself support connection indications
on connection-oriented protocols, so asyncore "fakes" the indication by detecting
if socket is writable. As the writable was false => no write event => no connection indication.

asyncore usage and documentation is bad, so when using the module, read the source
code to understand it's usage and functioning, or use other implementation eg.
Tornado.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Java NIO server and Python asyncore client foobarometer@gmail.com - 2013-02-05 00:09 -0800
  Re: Java NIO server and Python asyncore client dieter <dieter@handshake.de> - 2013-02-06 08:42 +0100
  Re: Java NIO server and Python asyncore client Petri Heinilä <hevi00@gmail.com> - 2013-02-15 01:41 -0800

csiph-web