add example using select/asyncore from http://etutorials.org/Programming/Python+tutorial/Part+IV+Network+and+Web+Programming/Chapter+19.+Sockets+and+Server-Side+Network+Protocol+Modules/19.3+Event-Driven+Socket+Programs/
This commit is contained in:
24
asyncore/TcpServer.py
Normal file
24
asyncore/TcpServer.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import asyncore
|
||||
import socket
|
||||
|
||||
class MainServerSocket(asyncore.dispatcher):
|
||||
def __init__(self, port):
|
||||
asyncore.dispatcher.__init__(self)
|
||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.bind(('',port))
|
||||
self.listen(5)
|
||||
def handle_accept(self):
|
||||
newSocket, address = self.accept( )
|
||||
print "Connected from", address
|
||||
SecondaryServerSocket(newSocket)
|
||||
|
||||
class SecondaryServerSocket(asyncore.dispatcher_with_send):
|
||||
def handle_read(self):
|
||||
receivedData = self.recv(8192)
|
||||
if receivedData: self.send(receivedData)
|
||||
else: self.close( )
|
||||
def handle_close(self):
|
||||
print "Disconnected from", self.getpeername( )
|
||||
|
||||
MainServerSocket(8881)
|
||||
asyncore.loop( )
|
||||
Reference in New Issue
Block a user