The Occasional Occurence

python simplicity

May 06, 2004 at 09:36 AM | categories: Python

Ok, needed to return only the numerical portion from a string like 'h00009492a802.ne.client2.attbi.com'. Pretty simple to do in Python:

def onlyNum(s):
    num = [n for n in s if n.isdigit()]
    return ''.join(num)

Voila. And the result?

>>> s = 'h00009492a802.ne.client2.attbi.com'
>>> onlyNum(s)
'000094928022'

The idea was to check the amount of numerical digits in a sending machines domain name in a mail header to help determine if it is SPAM. Since mostly spam originates from addresses like the one above, to me it would seem reasonable to say:

nums_from_header = onlyNum(header_hostname)
if len(nums_from_header) > 4:
    SPAM = True
else:
    SPAM = False

It's not perfect (and I wound up not using it in favor of a DNS blacklist), but Python makes it pretty easy to conceptualize.

(I'm glad that I can evangelize Python to all of you who come here to read stuff about Curtis. I can just see you all shaking your heads at this sort of thing. Makes me smile :-)

cw