-
-
Save debojitkakoti/380abb8d3c6c4499d8e7b7b15eb60d60 to your computer and use it in GitHub Desktop.
Async TCP server example (using standard python lib asyncore)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' Async TCP server to make first tests of newly received GPS trackers ''' | |
import asyncore | |
import socket | |
import logging | |
class Server(asyncore.dispatcher): | |
def __init__(self, address): | |
asyncore.dispatcher.__init__(self) | |
self.logger = logging.getLogger('Server') | |
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.set_reuse_addr() | |
self.bind(address) | |
self.address = self.socket.getsockname() | |
self.logger.debug('binding to %s', self.address) | |
self.listen(5) | |
def handle_accept(self): | |
# Called when a client connects to our socket | |
client_info = self.accept() | |
if client_info is not None: | |
self.logger.debug('handle_accept() -> %s', client_info[1]) | |
ClientHandler(client_info[0], client_info[1]) | |
class ClientHandler(asyncore.dispatcher): | |
def __init__(self, sock, address): | |
asyncore.dispatcher.__init__(self, sock) | |
self.logger = logging.getLogger('Client ' + str(address)) | |
self.data_to_write = [] | |
def writable(self): | |
return bool(self.data_to_write) | |
def handle_write(self): | |
data = self.data_to_write.pop() | |
sent = self.send(data[:1024]) | |
if sent < len(data): | |
remaining = data[sent:] | |
self.data.to_write.append(remaining) | |
self.logger.debug('handle_write() -> (%d) "%s"', sent, data[:sent].rstrip()) | |
def handle_read(self): | |
data = self.recv(1024) | |
self.logger.debug('handle_read() -> (%d) "%s"', len(data), data.rstrip()) | |
# !!!!! EAXAMPLE - ECHO | |
self.data_to_write.insert(0, data) | |
def handle_close(self): | |
self.logger.debug('handle_close()') | |
self.close() | |
def main(): | |
logging.basicConfig(level=logging.DEBUG, format='%(name)s:[%(levelname)s]: %(message)s') | |
HOST = '0.0.0.0' | |
PORT = 1507 | |
s = Server((HOST,PORT)) | |
asyncore.loop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment