2015-06-02 21:20:49 +02:00
parent 12b5878f54
commit 219ffcce0f
3 changed files with 95 additions and 0 deletions

24
asyncore/TcpServer.py Normal file
View 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( )