def startup(self): # When ReverseProxy is used the asyncore.loop() handles this part of the process... mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) if (logger): logger.info('%s.startup() :: self.ipAddr=[%s], self.port=[%s]' % (self.__class__,self.ipAddr,self.port)) mySocket.bind ( ( self.ipAddr, self.port) ) mySocket.listen ( 1 ) channel, details = mySocket.accept() if (logger): logger.info('(%s) Opened a connection with "%s".' % (self.port,details)) __data__ = '' __filename__ = None __fOut__ = None __chunk_count__ = 0 __re1__ = re.compile("@@@filename=(?P<filename>.*)@@@", re.DOTALL | re.MULTILINE) __re2__ = re.compile("@@@address=(?P<address>.*)@@@", re.DOTALL | re.MULTILINE) while True: try: data = channel.recv(8192) if (data) and (len(data) > 0): for ch in data: if (not __filename__) and (ord(ch) == 0): if (__data__) and (misc.isString(__data__)) and (len(__data__) > 0) and (callable(self.callback)): self.__handler__(__data__) __data__ = '' else: __data__ += ch matches1 = __re1__.search(__data__) matches2 = __re2__.search(__data__) if (matches1): f = matches1.groupdict().get('filename',None) if (f): __filename__ = f dirname = os.path.dirname(__filename__) if (callable(self.callback)): dirname2 = self.__handler__(dirname) if (misc.isIterable(dirname2) and (len(dirname2) > 0)): dirname2 = dirname2[0] if (dirname != dirname2): __filename__ = __filename__.replace(dirname,dirname2) dirname = dirname2 if (not os.path.exists(dirname)): os.makedirs(dirname) __fOut__ = open(__filename__,'wb') __data__ = '' elif (matches2): f = matches2.groupdict().get('address',None) if (f): self.address = f if (logger): logger.debug('Address is "%s".' % (self.address)) __data__ = '' else: i = __data__.find(self.eof) if (__filename__) and (i > -1): __data__ = [ch for ch in __data__] del __data__[i:] __data__ = ''.join(__data__) __fOut__.write(__data__) __fOut__.flush() __fOut__.close() if (callable(self.callback)): self.__handler__(__filename__) if (_utils.is_valid_ip_and_port(self.address)): connect_to_ip,connect_to_port = parse_ip_address_and_port(self.address, default_ip='0.0.0.0', default_port=51555) __writer__ = SocketWriter(connect_to_ip, connect_to_port,retry=True) __writer__.send('@@@delete=%s@@@' % (os.path.basename(__filename__))) __writer__.close() __filename__ = None __fOut__ = None __data__ = '' if (__filename__): __chunk_count__ += 1 if (logger): logger.debug('DEBUG: writing (%s bytes) x (%s) --> "%s"' % (len(__data__),__chunk_count__,__fOut__.name)) __fOut__.write(__data__) __data__ = '' except socket.error: mySocket.close() mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) if (logger): logger.info('%s.reconnect() :: self.ipAddr=[%s], self.port=[%s]' % (self.__class__,self.ipAddr,self.port)) mySocket.bind ( ( self.ipAddr, self.port) ) mySocket.listen ( 1 ) channel, details = mySocket.accept() if (logger): logger.info('(%s) Reopened a connection with "%s".' % (self.port,details)) __data__ = '' __filename__ = None __fOut__ = None __chunk_count__ = 0 except Exception, details: info_string = _utils.formattedException(details=details) if (logger): logger.exception('EXCEPTION: %s\n%s' % (details,info_string))
parser = CustomOptionParser("usage: %prog [options]") parser.add_option('-v', '--verbose', dest='verbose', help="verbose", action="store_true") parser.add_option("-i", "--input", action="store", type="string", help="Fully qualified filesystem path or IP address.", dest="ipath") parser.add_option("-o", "--output", action="store", type="string", help="Fully qualified filesystem path or IP address.", dest="opath") parser.add_option("-d", "--dest", action="store", type="string", help="Fully qualified filesystem path for incoming files.", dest="dest") parser.add_option("-r", "--retry", action="store_true", help="Sets retry to True otherwise will not retry connection with Remote.", dest="retry") parser.add_option("-l", "--listen", action="store", type="string", help="IP address and port for listener, provides bidirectional communications.", dest="listener") options, args = parser.parse_args() _isVerbose = False if (options.verbose): _isVerbose = True __ipath__ = None if (options.ipath and (os.path.exists(options.ipath) and os.path.isdir(options.ipath)) or (_utils.is_valid_ip_and_port(options.ipath)) ): __ipath__ = options.ipath if (_isVerbose): print >> sys.stdout, 'INFO: input path is "%s".' % (__ipath__) __opath__ = None if (options.opath and (os.path.exists(options.opath) and os.path.isdir(options.opath)) or (_utils.is_valid_ip_and_port(options.opath)) ): __opath__ = options.opath __dest__ = None if (options.dest and (os.path.exists(options.dest) and os.path.isdir(options.dest)) ): __dest__ = options.dest __retry__ = False if (options.retry):