Shortest gmail poplib python 2.7

Gmail works best in SSL mode. Find out the number of emails in your gmail inbox.

  1. Credentials are input from the keyboard, even with an extra example of hiding the password as opposed to placing the credentials in the program (which would be good for crontab scripts)
#!/usr/bin/python
import poplib,getpass

poPserver = poplib.POP3_SSL("pop.gmail.com", 995)
poPuser = raw_input('Username:')
print poPserver.user(poPuser)
print poPserver.pass_(getpass.getpass())
messageCount = len(poPserver.list()[1])
print messageCount, "messages"

shortest poplib example

shortest poplib example python 2.7

short python 2.7 poplib tutorial, python27 poplib library usage lesson, poplib example

Get it at GitHub: https://github.com/weblanta/pytbl-spe

#!/usr/bin/python

from poplib import *

poPserver = POP3('aaa.bbb.ccc.ddd') # ip works better than url
print poPserver.getwelcome() # todo: convert print to debug
print poPserver.user('email@address.com') # todo: chng print to dbg
print poPserver.pass_('EmailPassword') # todo: s/print/debug.validate(
messagesInfo = poPserver.list()[1]
numMessages = len(messagesInfo)

for i in range(numMessages):
  messageIs = poPserver.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

A longer example puts the login credentials up front and easier to get to here;

#!/usr/bin/python

from poplib import *

popIp='aaa.bbb.ccc.ddd'
popAddr='email@address.com'
popPass='EmailPassword'

popSrvr = POP3(popIp) # ip works better than url
print popSrvr.getwelcome() # todo: convert print to debug
print popSrvr.user(popAddr) # todo: chng print to dbg
print popSrvr.pass_(popPass) # todo: s/print/debug.validate(
messagesInfo = popSrvr.list()[1]
numMessages = len(messagesInfo)

for i in range(numMessages):
  messageIs = popSrvr.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

and now in this example we split it into a class;

#!/usr/bin/python

from poplib import *

popIp='aaa.bbb.ccc.ddd'
popAddr='email@address.com'
popPass='EmailPassword'

class poppins:

  def __init__(self):
    self.popSrvr = POP3(popIp) # ip works better than url
    print self.popSrvr.getwelcome() # todo: convert print to debug
    print self.popSrvr.user(popAddr) # todo: chng print to dbg
    print self.popSrvr.pass_(popPass) # todo: s/print/debug.validate(
    messagesInfo = self.popSrvr.list()[1]
    self.numMessages = len(messagesInfo)

popJones=poppins()
for i in range(popJones.numMessages):
  messageIs = popJones.popSrvr.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

finally, we move the credentials and the boiler-plate into a library

# class in the poppins thingamajig
from poplib import *

class poppinsMary:
    def __init__(self):
        popIp='aaa.bbb.ccc.ddd'
        popAddr='email@address.com'
        popPass='EmailPassword'

class poppins:

  def __init__(self):
    x=poppinsMary()
    self.popSrvr = POP3(x.popIp) # ip works better than url
    print self.popSrvr.getwelcome() # todo: convert print to debug
    print self.popSrvr.user(x.popAddr) # todo: chng print to dbg
    print self.popSrvr.pass_(x.popPass) # todo: s/print/debug.validate(
    messagesInfo = self.popSrvr.list()[1]
    self.numMessages = len(messagesInfo)

Which allows us a really short email reading python program here

#!/usr/bin/python

import popCredentials

popJones=popCredentials.poppins()
for i in range(popJones.numMessages):
  messageIs = popJones.popSrvr.retr(i+1)[1]
  print "==========================",
  print " message number",i,
  print "=========================="
  for j in messageIs:
    print j
    continue

Shortest Python Pyramid Virtual Environment

Init

sudo apt-get install virtualenv python-pip python-dev
pip install –upgrade pip setuptools
pip install pyramid gunicorn waitress

check pip settings in ~/.local/bin/pip

Python 3.6

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
python3.6
virtualenv –python=python3.6 name_of_ve_dir

Create Virtual Environment from Pyramid setup.py

virtualenv –python=python3.6 ve4e9
source ve4e9/bin/activate
cd MyProject
python setup.py install
pserve development.ini

Activate Virtual Environment

source ve4e9/bin/activate

Pyramid Bakery

pip install cookiecutter

Create simple

cookiecutter gh:Pylons/pyramid-cookiecutter-starter
pip install -e “.[testing]”
pserve development.ini

Create Alchemy

cookiecutter gh:Pylons/pyramid-cookiecutter-alchemy
pip install -e “.[testing]”
initialize_MyProject_db development.ini
pserve development.ini

Clone

git clone ssh://account@repositoryurl.com/~/MyProject

Invoke

cd MyProject
pserve development.ini

shortest Python timezones example

assuming we import datetime and pytz, just for a little bit of brevity ….

print datetime.datetime.utcnow().replace(tzinfo=pytz.utc).\
   astimezone(pytz.timezone("US/Eastern")).strftime("%Y-%m-%d %H:%M")

Whoa Nelly, Break it down bro! Below is an example of getting a ‘Naive’ time. I.E. it will be the time of the computer you are using, but have no associated timezone designation. This example also will get an error if you can’t import datetime.

import datetime
nowVariable = datetime.datetime.now()
print nowVariable

But a naive time object is unaware of the timezone, and the pytz object library is a great addition to datetime, but again, the next example will make your time object timezone aware, but first it will test that your computer can import the pytz object library.

import datetime, pytz
nowVariable = datetime.datetime.now()
laTimeVariable = nowVariable.replace(tzinfo=pytz.timezone("US/Pacific"))
print laTimeVariable

Other timezones could be US/Mountain, US/Central, Europe/Rome, Australia/Perth, Asia/Hong_Kong, etc.

Now, if we want to print the ‘aware’ date object in a pretty format, we can say;

print laTimeVariable.strftime("%Y-%m-%d %H:%M %Z")

This is great to populate the display variable when the date and time are sent to the screen. But with many different timezones and everything happening on the world wide dub dub dub all over the world at all hours, internally to my python code, I might want to always store and calculate the time in one single standard timezone, only converting as the last step to display it to a local user.

To get the current universal time, naive and aware;

utcNaiveVariable = datetime.datetime.utcnow()
utcAwareVariable = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)

Shortest Postfix sudo mail queue command list

Commands

mailq # note the message id – ten (10) alpha key to emails in queue

postcat -vq nnnnnnnnnn

postsuper -d nnnnnnnnnn

cd /etc/postfix; vi virtual; postmap virtual # forwarding

cd /etc/postfix; vi rbl_override; postmap rbl_override #whitelist

Postfix queues: pending and deferred.

Deferred queue is for messages that have soft-failed. Postfix will retry sending deferred emails.

Ubuntu 16 server setup

adduser {{username}}
adduser {{username}} sudo

apt-get install apache2
apt-get install php
apt-get install libapache2-mod-php
apt-get install php-mcrypt
apt-get install php-mysql

apt install iptables
apt install fail2ban

apt update
apt full-upgrade
apt install build-essential
apt install tcl

add some swap

fallocate -l 1G /mnt/1GB.swap
dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
mkswap /mnt/1GB.swap
swapon /mnt/1GB.swap
chmod 0600 /mnt/1GB.swap
echo "/mnt/1GB.swap  none  swap  sw 0  0" >>/etc/fstab

apt install mysql-server
mysql_secure_installation

{{su}}

curl latest redis.io stable
make
make test
make install
mkdir /etc/redis
cp redis.conf /etc/redis

create /etc/systemd/system/redis.service – e.g.

[Unit]
Description=Redis
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target

now you need to diff your redis.conf – password, persistence, binding, etc.

when you are ready to move … (or to test, see ExecStart)

systemctl enable redis

next

cd /etc/apache2
ln mods-available/rewrite.load mods-enabled/rewrite.load