def redis_set(key, value): '''set redis key to the given value''' global POOL redis_url = 'redis://%s' % get_container_host_ip() POOL = redis.ConnectionPool.from_url(redis_url) rds = redis.StrictRedis(connection_pool=POOL) rds.set(key, value) return value
def redis_get(key): '''returns the value of the given redis key''' global POOL redis_url = 'redis://%s' % get_container_host_ip() POOL = redis.ConnectionPool.from_url(redis_url) rds = redis.StrictRedis(connection_pool=POOL) value = rds.get(key) return value
def send_email(html_content, sender, receiver, subject): msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver email_message = MIMEText(html_content, 'html') msg.attach(email_message) s = smtplib.SMTP(get_container_host_ip()) # "smtp://%s:25" % get_container_host_ip() s.sendmail(sender, receiver, msg.as_string()) print("Email sent to: {}!".format(receiver)) s.quit()
def send_email(send_to, subject, body): '''send email with given inputs''' send_to = [str(email) for email in send_to] msg = MIMEText(body) send_to_str = ', '.join(send_to) #sender = '{0}@{1}'.format(getpass.getuser(), get_hostname()) sender = '*****@*****.**' msg['Subject'] = subject msg['From'] = sender msg['To'] = send_to_str print('Sending email.\nFrom: {0}\nTo: {1}\nSubject:: {2}\nBody: {3}'.format(sender, send_to, subject, body)) smtp_obj = smtplib.SMTP(get_container_host_ip()) smtp_obj.sendmail(sender, send_to, msg.as_string()) smtp_obj.quit()
def send_email(sender, cc_recipients, bcc_recipients, subject, body, attachments=None): """Send an email. All arguments should be Unicode strings (plain ASCII works as well). Only the real name part of sender and recipient addresses may contain non-ASCII characters. The email will be properly MIME encoded and delivered though SMTP to 172.17.0.1. This is easy to change if you want something different. The charset of the email will be the first one out of US-ASCII, ISO-8859-1 and UTF-8 that can represent all the characters occurring in the email. """ # combined recipients recipients = [] recipients.extend([cc_recipients, bcc_recipients]) # Header class is smart enough to try US-ASCII, then the charset we # provide, then fall back to UTF-8. header_charset = 'ISO-8859-1' # We must choose the body charset manually for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8': try: body.encode(body_charset) except UnicodeError: pass else: break # Split real name (which is optional) and email address parts sender_name, sender_addr = parseaddr(sender) parsed_cc_recipients = [parseaddr(rec) for rec in cc_recipients] parsed_bcc_recipients = [parseaddr(rec) for rec in bcc_recipients] #recipient_name, recipient_addr = parseaddr(recipient) # We must always pass Unicode strings to Header, otherwise it will # use RFC 2047 encoding even on plain ASCII strings. sender_name = str(Header(unicode(sender_name), header_charset)) unicode_parsed_cc_recipients = [] for recipient_name, recipient_addr in parsed_cc_recipients: recipient_name = str(Header(unicode(recipient_name), header_charset)) # Make sure email addresses do not contain non-ASCII characters recipient_addr = recipient_addr.encode('ascii') unicode_parsed_cc_recipients.append((recipient_name, recipient_addr)) unicode_parsed_bcc_recipients = [] for recipient_name, recipient_addr in parsed_bcc_recipients: recipient_name = str(Header(unicode(recipient_name), header_charset)) # Make sure email addresses do not contain non-ASCII characters recipient_addr = recipient_addr.encode('ascii') unicode_parsed_bcc_recipients.append((recipient_name, recipient_addr)) # Make sure email addresses do not contain non-ASCII characters sender_addr = sender_addr.encode('ascii') # Create the message ('plain' stands for Content-Type: text/plain) msg = MIMEMultipart() msg['CC'] = COMMASPACE.join([formataddr((recipient_name, recipient_addr)) for recipient_name, recipient_addr in unicode_parsed_cc_recipients]) msg['FROM'] = "*****@*****.**" msg['TO'] = "*****@*****.**" msg['BCC'] = COMMASPACE.join([formataddr((recipient_name, recipient_addr)) for recipient_name, recipient_addr in unicode_parsed_bcc_recipients]) msg['Subject'] = Header(unicode(subject), header_charset) msg.attach(MIMEText(body.encode(body_charset), 'plain', body_charset)) # Add attachments if isinstance(attachments, types.DictType): for fname in attachments: part = MIMEBase('application', "octet-stream") part.set_payload(attachments[fname]) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % fname) msg.attach(part) #print "#" * 80 #print msg.as_string() # Send the message via SMTP to docker host smtp_url = "smtp://%s:25" % get_container_host_ip() print "smtp_url : %s",smtp_url smtp = SMTP(get_container_host_ip()) smtp.sendmail(sender, recipients, msg.as_string()) smtp.quit()
def get_redis_endpoint(): """Return redis endpoint running on container host if running within a container or localhost if otherwise.""" return "redis://%s" % get_container_host_ip()