SESWatcher : how to prevent Amazon SES account from being suspended

Introdution

At this moment, AWS SES service is the best affordable price and high-quality service to sending (and receiving) programmatic emails. But, one day your account could be suspended without notice, and that takes a big effort of requesting support to get it back to work.

So it’s good to prevent that siatuation happens by watching your Bounces Rate and Complaints Rate. One way to resolve problem when your rates being too-high than allowance, is send a big amount of dummy emails to your blackhole email address.

Example

These is your send statistics :

  • Deliveries : 2000
  • Bounces : 180 ==> Bounces Rate : 180 / 2000 = 9%
  • Complaints : 30 ==> Complaints Rate : 30 / 2000 = 1.5%

Meaning :

  • Bounces : when your email didn’t send to existed email address (maybe your user type wrong or input fake one), we can pre-validate email before sending by using some email validator library or services like fast-email-validator
  • Complaints : when your receiver mark your email as SPAM :( Poor you !!!

Solution

AWS SES rules :

  • Bounces Rate : 5% => So you have to send more (180 / 4%) - 2000 = 2500 emails to reduce Bounces Rate to 4%
  • Complaints Rate : 0.1% => So you have to send more (30 / 0.07%) - 2000 = 40857 emails to reduce Complaints Rate to 0.07%

==> Number emails send = max(2500, 40857) = 40857 emails

SPAM YOURSELF 40857 EMAILS !!! 😂

SESWatcher - Your SES watchman

It’s open source software at https://github.com/khanhicetea/seswatcher (My first PyPi package :D)

Installation

1
$ pip install seswatcher

Usage

  1. Step 1 : Get AWS Credential User within AmazonSESFullAccess policy
  2. Step 2 : Verify sender email in AWS SES
  3. Step 3 : Get a blackhole email address which receives un-important emails.
  4. Step 4 : Create a hourly cronjob that runs seswatcher
1
$ seswatcher [OPTIONS] ACCESS_KEY SECRET_KEY FROM_EMAIL TO_EMAIL

Get help :

1
2
3
4
5
6
7
$ seswatcher --help
Usage: seswatcher [OPTIONS] ACCESS_KEY SECRET_KEY FROM_EMAIL TO_EMAIL

Options:
--region TEXT AWS SES Region (default is us-east-1)
--interval INTEGER Timer interval
--help Show this message and exit.

#TIL : Setter and getter behavior of class property in Python3

In previous TIL, I learned about the way to define getter and setter in Javascript

Today, I learned it in Pythonic way ;) (in Python3)

So here is example (the easy way to learn from a code) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class A:
def __init__(self,x):
print ('init', x)
self.x = x

@property
def x(self):
print ('getter')
return self.__x

@x.setter
def x(self, x):
print ('setter', x)
if x < 0:
self.__x = 0
elif x % 2 == 0:
self.__x = x
else:
self.__x = x * 2

a = A(10)
print(a.x)
a.x = -1
print(a.x)
a.x = 2
print(a.x)
a.x = 7
print(a.x)

Output :

1
2
3
4
5
6
7
8
9
10
11
12
13
init 10
setter 10
getter
10
setter -1
getter
0
setter 2
getter
2
setter 7
getter
14

This is so cool feature, and even it has deleter property method, which triggers when we run del object.property :D

In reactive JS framework that they use getter and setter like a core of Reactive, let find out more next articles ;)

#TIL : Context Managers in Python

In Python, sometimes you will see this syntax

1
2
with something:
do_something_else(something)

Then you ask yourself, why I have to use this with syntax ? What runs inside that statement ?

Here is how it works, it’s called Context Managers in object

You can define context managers for a class of object to make sure some logic runs correctly without forgeting

Example :

1
2
3
4
5
6
7
8
9
10
11
12
13
class Animal:
def __init__(self, name):
self.name = name

def __enter__(self):
print("Enter the room !")

def __exit__(self, exc_type, exc_value, traceback):
print("Exit the room !")

kitty = Animal("Kitty Kat")
with kitty:
print(kitty.name)

Result

1
2
3
Enter the room !
Kitty Kat
Exit the room !

So, when you start using with keyword on a object, it runs __enter__ method, when everything inside with block is runned, __exit__ will be called !

It’s cool feature of Python !

#TIL : Do not use mutable objects as default parameters

I learned this from learn-python3

Example :

1
2
3
4
5
6
7
8
9
10
def append_if_multiple_of_five(number, magical_list=[]):
if number % 5 == 0:
magical_list.append(number)
return magical_list

print(append_if_multiple_of_five(100))
print(append_if_multiple_of_five(105))
print(append_if_multiple_of_five(123))
print(append_if_multiple_of_five(123, []))
print(append_if_multiple_of_five(123))

Result :

1
2
3
4
5
[100]
[100, 105]
[100, 105]
[]
[100, 105]

So default parameters in Python are shared between function calls if it isn’t passed from caller. So be careful because shared mutable object can affect your logic between function calls, where MAGIC was born !

One safe way to achieve the goal, use None as replacement like this

1
2
3
4
5
6
7
8
9
10
11
12
def append_if_multiple_of_five(number, magical_list=None):
if not magical_list:
magical_list = []
if number % 5 == 0:
magical_list.append(number)
return magical_list

print(append_if_multiple_of_five(100))
print(append_if_multiple_of_five(105))
print(append_if_multiple_of_five(123))
print(append_if_multiple_of_five(123, []))
print(append_if_multiple_of_five(123))

Result :

1
2
3
4
5
[100]
[105]
[]
[]
[]

#TIL : Exporting environment variables on virtual env activate

You can put common environment variables to the file venv/bin/activate. So everytime we active the virtual env, everything is on the way

1
2
3
4
5
6
7
# venv/bin/active content

# export your env vars here
export FLASK_APP=hello
export FLASK_ENV=development
export DATABASE=hello.sqlite3
export SECRET_KEY=secret_key_here

#TIL : Simple HTTP server function helper

I use python3 (3.4+) to create a bash function to help me start quickly a simple http server on specified port

1
2
3
4
5
function server() {
local port="${1:-8000}" # Getting port number
google-chrome "http://127.0.0.1:$port" # Open URL in browser, could change to firefox --new-tab "http://127.0.0.1:$port"
python3 -m http.server $port --bind 127.0.0.1
}

Reading NFC Card ID on Ubuntu

Prerequisites

Install driver

My device is ACS122U : http://www.acs.com.hk/en/products/3/acr122u-usb-nfc-reader/#tab_downloads

Kernel modules

1
2
3
# echo "uinput" >> /etc/modules
# echo "install nfc /bin/false" >> /etc/modprobe.d/blacklist.conf
# echo "install pn533 /bin/false" >> /etc/modprobe.d/blacklist.conf

Install packages

1
sudo apt-get install swig libccid pcscd libpcsclite-dev libpcsclite1 python-dev python-pip gcc linux-headers-$(uname -r)

Run service

1
sudo service pcscd restart

Install python packages

1
sudo pip install pyscard python-uinput evdev

Source code

This is source code that read the card ID and simulate keystrokes to type card ID by text then pressing ENTER key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /usr/bin/env python

from __future__ import print_function
from time import sleep

from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
from smartcard.CardMonitoring import CardMonitor, CardObserver
from smartcard.util import toHexString
from evdev import UInput, ecodes
import uinput


device = uinput.Device([uinput.KEY_0, uinput.KEY_1, uinput.KEY_2, uinput.KEY_3, uinput.KEY_4, uinput.KEY_5, uinput.KEY_6, uinput.KEY_7, uinput.KEY_8, uinput.KEY_9, uinput.KEY_A, uinput.KEY_B, uinput.KEY_C, uinput.KEY_D, uinput.KEY_E, uinput.KEY_F, uinput.KEY_G, uinput.KEY_H, uinput.KEY_I, uinput.KEY_J, uinput.KEY_K, uinput.KEY_L, uinput.KEY_M, uinput.KEY_N, uinput.KEY_O, uinput.KEY_P, uinput.KEY_Q, uinput.KEY_R, uinput.KEY_S, uinput.KEY_T, uinput.KEY_U, uinput.KEY_V, uinput.KEY_W, uinput.KEY_X, uinput.KEY_Y, uinput.KEY_Z, uinput.KEY_TAB])


def card_id_to_keyboards(card_id):
id = card_id.replace(' ', '')

return [getattr(uinput, 'KEY_'+x.upper()) for x in id]


# a simple card observer that tries to select DF_TELECOM on an inserted card
class selectDFTELECOMObserver(CardObserver):
"""A simple card observer that is notified
when cards are inserted/removed from the system and
prints the list of cards
"""

def __init__(self):
self.observer = ConsoleCardConnectionObserver()

def update(self, observable, actions):
try:
(addedcards, removedcards) = actions
for card in addedcards:
print("+Inserted: ", toHexString(card.atr))
card.connection = card.createConnection()
card.connection.connect()
card.connection.addObserver(self.observer)
apdu = [0xFF, 0xCA, 0x00, 0x00, 0x00]
response, sw1, sw2 = card.connection.transmit(apdu)
keys = card_id_to_keyboards(toHexString(response))
for key in keys:
device.emit_click(key)
ui = UInput()
ui.write(ecodes.EV_KEY, ecodes.KEY_ENTER, 0)
ui.write(ecodes.EV_KEY, ecodes.KEY_ENTER, 1)
ui.syn()
ui.close()
for card in removedcards:
print("-Removed: ", toHexString(card.atr))
except:
pass

if __name__ == '__main__':
print("Insert or remove a SIM card in the system.")
print("This program will exit in 60 seconds")
print("")
cardmonitor = CardMonitor()
selectobserver = selectDFTELECOMObserver()
cardmonitor.addObserver(selectobserver)

sleep(86400)

# don't forget to remove observer, or the
# monitor will poll forever...
cardmonitor.deleteObserver(selectobserver)

import sys
if 'win32' == sys.platform:
print('press Enter to continue')
sys.stdin.read(1)

Run

1
$ sudo python main.py

Demo

This is my demo application that using card ID like a user password, so I can unlock my computer or enter sudo mode :D

It Just Works

If you want a “just-work” solution, checkout this repo. It is built on Java smartcardio.

1
$ java -jar rfid-reader2keyboard.jar

Good luck ! ;)