Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- # Taken from http://tfletcher.com/lib/rfc822.py
- qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
- dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
- atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
- quoted_pair = '\\x5c[\\x00-\\x7f]'
- domain_literal = "[\\x5b](?:%s|%s)*[\\x5d]" % (dtext, quoted_pair)
- quoted_string = "\\x22(?:%s|%s)*\\x22" % (qtext, quoted_pair)
- domain_ref = atom
- sub_domain = "(?:%s|%s)" % (domain_ref, domain_literal)
- word = "(?:%s|%s)" % (atom, quoted_string)
- domain = "%s(?:\\x2e%s)*" % (sub_domain, sub_domain)
- local_part = "%s(?:\\x2e%s)*" % (word, word)
- # Adding maximum length restrictions
- addr_spec = "(?=^.{1,256}$)(?=.{1,64}@)%s\\x40%s" % (local_part, domain)
- email_address = re.compile('^%s$' % addr_spec)
- if __name__ == '__main__':
- list_of_good = ['somewhere@over.the.rainbow',
- 'SOMEWHERE@over.the.rainbow',
- 'SOMEWHERE@OVER.THE.RAINBOW',
- 'foo@example.com',
- 'paul@io',
- 'foo@bar.example.com',
- 'foo+bar@example.com',
- 'foo.bar@example.com',
- 'foo.bar@[127.0.0.1]',
- 'foo_bar@example.com',
- 'foo-bar@example.com',
- 'cal+henderson@iamcalx.com',
- '"cal henderson"@iamcalx.com',
- 'cal@[hello world].com',
- '"foo.bar.@.foo.com"@example.com',
- ('foo.bar'*9) + '@example.com']
- list_of_bad = ['somewhere.over.the.rainbow',
- 'foo..bar@example.com',
- '.foo@example.com',
- 'bar.@example.com',
- 'foo bar@example.com',
- 'bar@example com',
- 'bar@another example.com',
- 'foo@bar@test@example.com',
- # Local part can't exceed 64 chars
- ('foo.bar'*10) + '@example.com',
- # Total can't exceed 256
- 'foo.bar.foo.bar.foo.bar.bar@' + ('example'*40)]
- for t in list_of_good:
- assert email_address.match(t)
- for t in list_of_bad:
- assert email_address.match(t) is None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement