def run(self): """ Main entry point """ # Read command line usage = "%prog [options] <url> <address> [<address2>] [<address...>]" parser = OptionParser(usage=usage, version=self.NAME + ' ' + self.VERSION) parser.add_option("--http-user", dest="http_user", help="Username for HTTP POST authentication") parser.add_option("--http-pass", dest="http_pass", help="Password for HTTP POST authentication") parser.add_option("-s", "--smtp", dest="smtp", help="SMTP server address. Default: localhost", default='localhost') parser.add_option("--smtp-user", dest="smtp_user", help="Username for SMTP authentication") parser.add_option("--smtp-pass", dest="smtp_pass", help="Password for SMTP authentication") parser.add_option("-c", "--cc", dest="cc", help="Carbon Copy recipient") parser.add_option("-f", "--from", dest="sender", help="eMail sender. Default: emailer@localhost", default="emailer@localhost") parser.add_option( "-j", "--subject", dest="subject", help="eMail Subject. Default: MailWWW Autogenerated Mail", default="MailWWW Autogenerated Mail") parser.add_option("-n", "--no-css", dest="nocss", help="Disable embedding of linked Style Sheets", default=False, action="store_true") parser.add_option( "-m", "--multiple", dest="multiple", help= "Send multiple emails: one for each recipient (Cc field is ignored)", default=False, action="store_true") parser.add_option("-v", "--verbose", dest="verbose", help="Show progress information", default=False, action="store_true") (options, args) = parser.parse_args() # Parse mandatory arguments if len(args) < 2: parser.error("unvalid number of arguments") dest = [] i = 0 for a in args: if i == 0: url = a else: dest.append(a) i += 1 # Parse optional arguments http_user = options.http_user http_pass = options.http_pass cc = [] if options.cc: cc.append(options.cc) host = options.smtp port = 25 user = options.smtp_user pwd = options.smtp_pass sender = options.sender subject = options.subject nocss = options.nocss multiple = options.multiple verbose = options.verbose logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) # Opens URL logging.info('Fetching url %s', url) data = None if http_user or http_pass: # Use POST authentication data = urllib.urlencode({ 'username': http_user, 'password': http_pass, 'login': True }) f = urllib.urlopen(url, data) html = f.read() # Search for meta content-type tag, use this encoding when found encre = re.compile( r'<meta\s+http-equiv=(?:"|\')Content-Type(?:"|\')\s+content=(?:"|\')([^\'"]*)(?:"|\')\s*/>', re.I | re.M) match = encre.search(html) if match: encoding = self.__parseEncoding(match.group(1)) try: html = unicode(html, encoding, errors='replace') except LookupError as e: encoding = self.__parseEncoding(f.headers['content-type']) html = unicode(html, encoding, errors='replace') else: encoding = self.__parseEncoding(f.headers['content-type']) html = unicode(html, encoding, errors='replace') logging.info('Detected charset: %s', encoding) f.close() # Retrieve linked style sheets if not nocss: logging.info('Fetching Style Sheets...') parser = CSSLister(url) parser.feed(html) parser.close() for search, replace in parser.get_replacements(): html = html.replace(search, replace, 1) # Prepare mail msg = MIMEMultipart() msg['Date'] = formatdate(localtime=True) msg['Message-ID'] = make_msgid('emailer') msg['Subject'] = subject msg['From'] = sender if cc and not multiple: msg['Cc'] = ', '.join(cc) msg.preamble = 'This is a milti-part message in MIME format.' txt = MIMEText(html.encode('utf-8'), 'html', 'utf-8') msg.attach(txt) if not multiple: msg['To'] = ', '.join(dest) # Sends message smtp = smtplib.SMTP() smtp.connect(host, port) if user: smtp.login(user, pwd) if multiple: for d in dest: del msg['To'] msg['To'] = d logging.info('Sending mail to: %s', d) smtp.sendmail(sender, d, msg.as_string()) else: logging.info('Sending mail to: %s, Cc: %s', dest, cc) smtp.sendmail(sender, dest + cc, msg.as_string()) smtp.quit()
options.path = '/'+options.path conn = httplib.HTTPConnection("localhost:29392") conn.request(method,options.action+options.path) resp = conn.getresponse() data = resp.read() if options.action == '/download': print sys.getsizeof(data) exit() parser = ET.XMLTreeBuilder() parser.feed(data) tree = ET.ElementTree(parser.close()) root = tree.getroot() #print root.text ET.dump(tree) if options.action == '/list' and method == "GET": print "File listing: " file_listing(options.path, root) if options.action == '/tagdata' and method == "GET": print "Tag Data: " tag_data("/", root, options)
print "\n\n\f" except IOError: sys.exit(0) # broken stdout => broken pipe except Exception as e: sys.stderr.write("\nError extracting article: " + str(e) + "\n") if __name__ == "__main__": try: parser = OptionParser(usage="usage: %s [-d output-directory] <stdin>" % sys.argv[0]) parser.add_option("-d", action="store", type="string", dest="directory", help="directory where to store the articles") (options, args) = parser.parse_args() if len(args) > 0: print parser.usage exit(1) htmlparser = HTMLParser.HTMLParser() if options.directory != None: do_article = lambda a: save_article(htmlparser, a, options.directory) else: do_article = lambda a: print_article(htmlparser, a) parser = WikiParser(do_article) parser.process() parser.close() except KeyboardInterrupt: sys.stderr.write("\n\nCancelled. Partial results may have been generated.\n") exit(1)
def run(self): """ Main entry point """ # Read command line usage = "%prog [options] <url> <address> [<address2>] [<address...>]" parser = OptionParser(usage=usage, version=self.NAME + ' ' + self.VERSION) parser.add_option("--http-user", dest="http_user", help="Username for HTTP POST authentication") parser.add_option("--http-pass", dest="http_pass", help="Password for HTTP POST authentication") parser.add_option("-s", "--smtp", dest="smtp", help="SMTP server address. Default: localhost", default='localhost') parser.add_option("--smtp-user", dest="smtp_user", help="Username for SMTP authentication") parser.add_option("--smtp-pass", dest="smtp_pass", help="Password for SMTP authentication") parser.add_option("-c", "--cc", dest="cc", help="Carbon Copy recipient") parser.add_option("-f", "--from", dest="sender", help="eMail sender. Default: emailer@localhost", default="emailer@localhost") parser.add_option("-j", "--subject", dest="subject", help="eMail Subject. Default: MailWWW Autogenerated Mail", default="MailWWW Autogenerated Mail") parser.add_option("-n", "--no-css", dest="nocss", help="Disable embedding of linked Style Sheets", default=False, action="store_true") parser.add_option("-m", "--multiple", dest="multiple", help="Send multiple emails: one for each recipient (Cc field is ignored)", default=False, action="store_true") parser.add_option("-v", "--verbose", dest="verbose", help="Show progress information", default=False, action="store_true") (options, args) = parser.parse_args() # Parse mandatory arguments if len(args) < 2: parser.error("unvalid number of arguments") dest = [] i = 0 for a in args: if i == 0: url = a else: dest.append(a) i += 1 # Parse optional arguments http_user = options.http_user http_pass = options.http_pass cc = [] if options.cc: cc.append(options.cc) host = options.smtp port = 25 user = options.smtp_user pwd = options.smtp_pass sender = options.sender subject = options.subject nocss = options.nocss multiple = options.multiple verbose = options.verbose logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) # Opens URL logging.info('Fetching url %s', url) data = None if http_user or http_pass: # Use POST authentication data = urllib.urlencode({ 'username': http_user, 'password': http_pass, 'login': True }) f = urllib.urlopen(url, data) html = f.read() # Search for meta content-type tag, use this encoding when found encre = re.compile(r'<meta\s+http-equiv=(?:"|\')Content-Type(?:"|\')\s+content=(?:"|\')([^\'"]*)(?:"|\')\s*/>', re.I | re.M) match = encre.search(html) if match: encoding = self.__parseEncoding(match.group(1)) try: html = unicode(html, encoding, errors='replace') except LookupError as e: encoding = self.__parseEncoding(f.headers['content-type']) html = unicode(html, encoding, errors='replace') else: encoding = self.__parseEncoding(f.headers['content-type']) html = unicode(html, encoding, errors='replace') logging.info('Detected charset: %s', encoding) f.close() # Retrieve linked style sheets if not nocss: logging.info('Fetching Style Sheets...') parser = CSSLister(url) parser.feed(html) parser.close() for search, replace in parser.get_replacements(): html = html.replace(search, replace, 1) # Prepare mail msg = MIMEMultipart() msg['Date'] = formatdate(localtime=True) msg['Message-ID'] = make_msgid('emailer') msg['Subject'] = subject msg['From'] = sender if cc and not multiple: msg['Cc'] = ', '.join(cc) msg.preamble = 'This is a milti-part message in MIME format.' txt = MIMEText(html.encode('utf-8'), 'html', 'utf-8') msg.attach(txt) if not multiple: msg['To'] = ', '.join(dest) # Sends message smtp = smtplib.SMTP() smtp.connect(host, port) if user: smtp.login(user, pwd) if multiple: for d in dest: del msg['To'] msg['To'] = d logging.info('Sending mail to: %s', d) smtp.sendmail(sender, d, msg.as_string()) else: logging.info('Sending mail to: %s, Cc: %s', dest, cc) smtp.sendmail(sender, dest+cc, msg.as_string()) smtp.quit()