Java NIO server and Python asyncore client
Petri Heinilä
hevi00 at gmail.com
Fri Feb 15 04:41:21 EST 2013
On Tuesday, February 5, 2013 10:09:28 AM UTC+2, foobar... at 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.
More information about the Python-list
mailing list