class CmdProtocol(LineReceiver): delimiter = '\n' def __init__(self): self.client_ip = None self.handler = None def connectionMade(self): self.handler = ServerHandler() log.msg(self.transport) self.client_ip = self.transport.getPeer().host log.msg("Client connection from %s" % self.client_ip) if len(self.factory.clients) >= self.factory.clients_max: log.msg("Too many connections. bye !") self.client_ip = None self.transport.loseConnection() else: self.factory.clients.append(self.client_ip) self.transport.write("test") log.msg("Current client number is %d" % len(self.factory.clients)) def connectionLost(self, reason): log.msg('Lost client connection. Reason: %s' % reason) if self.client_ip: self.factory.clients.remove(self.client_ip) log.msg("Current client number is %d" % len(self.factory.clients)) def lineReceived(self, line): msg = json.loads(line) method = msg["method"] params = msg["params"] self.handler.run(method, params)
def connectionMade(self): self.handler = ServerHandler() log.msg(self.transport) self.client_ip = self.transport.getPeer().host log.msg("Client connection from %s" % self.client_ip) if len(self.factory.clients) >= self.factory.clients_max: log.msg("Too many connections. bye !") self.client_ip = None self.transport.loseConnection() else: self.factory.clients.append(self.client_ip) self.transport.write("test") log.msg("Current client number is %d" % len(self.factory.clients))
def clientAppend(self, sock, addr): while len(GlobalVal.clients.items()) > MAX_CONNECTION: time.sleep(10) sock.setblocking(0) self.sockets.append(sock) # 新建一个连接对象 client = ServerHandler(sock, addr) GlobalVal.clients[sock] = client logging.info("Client (%s, %s) connected to server" % addr) logging.info("%d client connections in total" % len(GlobalVal.clients.items()))
def handle(self): # 执行的时这个handler(),而不是父类的handler() """Handle a single HTTP request""" from handler import ServerHandler self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return # Avoid passing the raw file object wfile, which can do partial # writes (Issue 24291) stdout = BufferedWriter(self.wfile) try: handler = ServerHandler(self.rfile, stdout, self.get_stderr(), self.get_environ()) handler.request_handler = self # backpointer for logging handler.run(self.server.get_app()) # demo_app finally: stdout.detach()
def main(): opt = parse_options() server = ServerHandler(opt.localhost, opt.localport) server.proxy(opt.remotehost, opt.remoteport, opt.receive_first)
import socket from handler import ServerHandler HOST = "" PORT = 1998 try: svHandler = ServerHandler(HOST, PORT) svHandler.start() svHandler.join() except KeyboardInterrupt: print("") print("Encerrando o servidor...") svHandler.stop() with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) print("Goodbye.")
import socket from handler import ServerHandler import time HOST = 'localhost' PORT = 1232 try: server_handler = ServerHandler(HOST, PORT) #server_handler.daemon =True server_handler.start() #server_handler.join() while True: time.sleep(100) except (KeyboardInterrupt, SystemExit): print('') print('Encerrando o servidor...') server_handler.stop() with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) print('Hasta la vista baby!')