def get_host_ip(self): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] except Exception as e: logger.error(e) finally: s.close() return ip
def send(self, __send, addr): ''' 使用send 向 addr发送数据 :param send: protocal :param addr: 地址 :return: ''' try: __send.send_apply(self.pack_data, addr) except Exception as e: logger.error(e)
def datagramReceived(self, datagram, addr): # print(datagram) # 取出前16个字节,拿到元祖第一个内容,解码utf-8,去除空格 usage = struct.unpack("!16s", datagram[0:16])[0].decode('utf-8').strip('\x00') try: function_ = functools.partial(getattr(self, usage + "_handle"), datagram, addr) d = threads.deferToThread(function_) d.addErrback(lambda m: print(m)) except AttributeError as e: logger.error(e)
def datagramReceived(self, datagram, addr): # datagram 是以utf-8编码格式的bytes类型 usage = struct.unpack("!16s", datagram[0:16])[0].decode('utf-8').strip('\x00') print(usage) # type_name = struct.unpack("!16s", datagram[16:32])[0].decode('utf-8').strip('\x00') try: function_ = functools.partial(getattr(self, usage + "_handle"), datagram, addr) d = threads.deferToThread(function_) d.addCallback(self.success_or_failure) d.addErrback(lambda m: print(m)) except AttributeError as e: logger.error(e)
def get_host_ip(self): try: eth = ni.ifaddresses('eth0') if 2 in eth: ip = eth.get(2)[0].get('addr') else: ip = "127.0.0.1" except Exception as e: logger.error(e) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] except Exception as e: logger.error(e) return '0.0.0.0' finally: s.close() return ip
#!/usr/bin/env python from mylogging import logger def enable_debug(): import logging # 'stdout' is the name of a handler defined in .logger loggers = filter( lambda h: h.get_name() == 'stdout', logger.handlers ) loggers[0].setLevel( logging.DEBUG ) logger.debug("debug message") enable_debug() logger.debug("debug message") logger.info("info message") logger.warning("warning message") logger.error("error message") logger.critical("critical message")
def send(self, __send, addr): try: __send.send_apply(bytes(self), addr) except Exception as e: logger.error(e)
def send(self, __send, addr): try: __send.send_apply(self.pack_data, addr) except Exception as e: logger.error(e)
#!/usr/bin/env python from mylogging import logger def enable_debug(): import logging # 'stdout' is the name of a handler defined in .logger loggers = filter(lambda h: h.get_name() == 'stdout', logger.handlers) loggers[0].setLevel(logging.DEBUG) logger.debug("debug message") enable_debug() logger.debug("debug message") logger.info("info message") logger.warning("warning message") logger.error("error message") logger.critical("critical message")
def send_apply(self, order, addr): logger.info(order) try: self.transport.write(order, addr) except Exception as e: logger.error(e)