We love sharing our thoughts and technical knowledge with others, as we think information should be free. We also feel warm and fuzzy if we can help somebody with our writings.

Better crossdomain snippet for flask

Here’s a Flask crossdomain snippet, this sets appropriate HTTP Access Control headers. This one is more API-friendly than the one on Flask Snippets.

# -*- coding: utf-8 -*-
from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper

def crossdomain(origin=None, methods=None, headers=None,
                max_age=21600, attach_to_all=True,
    if methods is not None:
        methods = ', '.join(sorted(x.upper() for x in methods))
    if headers is not None and not isinstance(headers, basestring):
        headers = ', '.join(x.upper() for x in headers)
    if not isinstance(origin, basestring):
        origin = ', '.join(origin)
    if isinstance(max_age, timedelta):
        max_age = max_age.total_seconds()

    def get_methods():
        if methods is not None:
            return methods

        options_resp = current_app.make_default_options_response()
        return options_resp.headers['allow']

    def decorator(f):
        def wrapped_function(*args, **kwargs):
            if automatic_options and request.method == 'OPTIONS':
                resp = current_app.make_default_options_response()
                resp = make_response(f(*args, **kwargs))
            if not attach_to_all and request.method != 'OPTIONS':
                return resp

            h = resp.headers
            h['Access-Control-Allow-Origin'] = origin
            h['Access-Control-Allow-Methods'] = get_methods()
            h['Access-Control-Max-Age'] = str(max_age)
            h['Access-Control-Allow-Credentials'] = 'true'
            h['Access-Control-Allow-Headers'] = \
                "Origin, X-Requested-With, Content-Type, Accept, Authorization"
            if headers is not None:
                h['Access-Control-Allow-Headers'] = headers
            return resp

        f.provide_automatic_options = False
        return update_wrapper(wrapped_function, f)
    return decorator

Jussi Räsänen

C++, Python, Scala / Django, Flask.


Backup MX, you & Postfix

This guide will help you to play nice with others by not losing your incoming emails. All you will need is a primary mail -server, a valid reverse dns record, your domains and time.

Lets assume that you have a slick new domain called foobar.com and that you want to increase redundancy, since your primary mail-server might have outages from time to time.

Your primary email-server mail.example.com needs a buddy to keep hundreds of emails from your fans from bouncing back to the sender.

We shall call the new backup mx mail2.example.com.

Mail relay settings

Open your Postfix main configuration

$ vim /etc/postfic/main.cf

Next add foobar.com to your relay domain list. This setting controls the domains that should be relayed, Never leave this list empty, otherwise you will have an open relay for spammers.

relay_domains = foobar.com, $mydestination

Recipient restrictions to prevent spam

Here we define some simple rules to prevent misuse and spam.[1][2]

# Remember that the order of recipient restriction
# rules is important!
smtpd_recipient_restrictions =
    reject_rbl_client zen.spamhaus.org,

# Requires SMTP client to introduce themselves
smtpd_helo_required = yes

# Delaying rejects will allow Postfix
# to log rejected message info
smtpd_delay_reject = yes

# vrfy can be used to harvest emails
disable_vrfy_command = yes

strict_rfc821_envelopes = yes
unknown_address_reject_code  = 554
unknown_hostname_reject_code = 554
unknown_client_reject_code = 554
smtpd_data_restrictions =

And you’re done.

$ service postfix restart

References below

Jussi Räsänen

C++, Python, Scala / Django, Flask.