
Python 이메일 주소 및 MIME 파싱 라이브러리
.. image:: https://travis-ci.org/mailgun/flanker.svg?branch=master :target: https://travis-ci.org/mailgun/flanker
.. image:: https://coveralls.io/repos/github/mailgun/flanker/badge.svg?branch=master :target: https://coveralls.io/github/mailgun/flanker?branch=master
Flanker는 Mailgun 팀이 Python으로 작성한 오픈소스 파싱 라이브러리입니다.
Flanker는 현재 주소 파싱 라이브러리(flanker.addresslib)와 MIME 파싱 라이브러리(flanker.mime)로 구성되어 있습니다.
자세한 문서는 사용자 매뉴얼 <https://github.com/mailgun/flanker/blob/master/docs/User%20Manual.md>_ 및
API 참조 <https://github.com/mailgun/flanker/blob/master/docs/API%20Reference.md>_ 에서 제공됩니다. 아래에 빠른 시작 가이드가 있습니다.
Flanker는 프로덕션 환경에서 Python 2.7과 함께 Mailgun <www.mailgun.com>_ 에서 많이 사용됩니다. 현재 프로덕션 버전은 v0.8.5입니다.
Python 3 지원은 커뮤니티의 요청에 따라 v0.9.0에서 추가되었습니다. 저희 내부에서는 Python 3로 Flanker를 사용하지 않습니다. 저희가 아는 것은 테스트가 Python 3.6에서 통과한다는 것뿐이므로, 사용에 따른 책임은 본인에게 있습니다. Python 3 관련 문제가 있으면 언제든지 보고해 주세요.
pip로 flanker를 설치하거나 GitHub에서 저장소를 클론할 수 있습니다.
flanker로 작업을 시작하기 전에 Python 헤더 파일이 필요하므로 먼저 설치하세요:
.. code-block:: bash
sudo apt-get install python-dev
sudo yum install python-devel
pip를 사용하는 경우 다음과 같이 입력하세요:
.. code-block:: bash
pip install flanker
GitHub에서 클론하는 경우 다음과 같이 입력하세요:
.. code-block:: bash
git clone [email protected]:mailgun/flanker.git cd flanker pip install -e .
단일 사서함(표시 이름 및 이메일 주소)을 파싱하려면:
.. code-block:: py
from flanker.addresslib import address
address.parse('Foo [email protected]') Foo [email protected]
잘못된 주소는 None으로 반환됩니다:
.. code-block:: py
from flanker.addresslib import address
print address.parse('@example.com') None
단일 이메일 주소(표시 이름 없음)를 파싱하려면:
.. code-block:: py
from flanker.addresslib import address
address.parse('[email protected]', addr_spec_only=True) [email protected]
주소 목록을 파싱하려면:
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com']) [[email protected], [email protected]]
주소 목록을 파싱하고, 파싱된 주소와 파싱할 수 없는 부분이 포함된 튜플을 반환하려면
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com'], as_tuple=True) [[email protected], [email protected]], ['@example.com']
엄격 모드에서 주소 목록을 파싱하려면:
.. code-block:: py
from flanker.addresslib import address
address.parse_list(['[email protected], [email protected], @example.com'], strict=True) [[email protected], [email protected]]
이메일 주소를 검증하려면(파싱 및 DNS, MX 존재 여부, ESP 문법 검사):
.. code-block:: py
from flanker.addresslib import address
address.validate_address('[email protected]') [email protected]
주소 목록을 검증하려면:
.. code-block:: py
from flanker.addresslib import address
address.validate_list(['[email protected], [email protected], @mailgun.com'], as_tuple=True) ([[email protected], [email protected]], ['@mailgun.com'])
다음 예제에서는 message_string이 다음 MIME 메시지로 설정됩니다:
::
MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11c1d71697c7f004e6856996 From: Bob [email protected] To: Alice [email protected] Subject: hello, world Date: Mon, 16 Sep 2013 12:43:03 -0700
--001a11c1d71697c7f004e6856996 Content-Type: text/plain; charset=us-ascii
Hello, Alice
--001a11c1d71697c7f004e6856996 Content-Type: text/html; charset=us-ascii
Hello, Alice
--001a11c1d71697c7f004e6856996--
MIME 메시지를 파싱하려면:
.. code-block:: py
from flanker import mime
msg = mime.from_string(message_string)
MIME 메시지 헤더(헤더가 포함된 유니코드 다중값 사전):
.. code-block:: py
from flanker import mime
msg = mime.from_string(message_string) msg.headers.items() [('Mime-Version', '1.0'), ('Content-Type', ('multipart/alternative', {'boundary': u'001a11c1d71697c7f004e6856996'})), ('From', 'Bob [email protected]'), ('To', 'Alice [email protected]'), ('Subject', 'hello, world'), ('Date', 'Mon, 16 Sep 2013 12:43:03 -0700')]
유용한 content_type 멤버 및 조건자(predicate):
.. code-block:: py
from flanker import mime msg = mime.from_string(message_string)
msg.content_type.is_multipart() True
msg.content_type.is_singlepart() False
msg.content_type.is_message_container() False
메시지의 디코딩된 본문:
.. code-block:: py
from flanker import mime msg = mime.from_string(message_string)
None because message is multipart
print msg.body None
for part in msg.parts: print 'Content-Type: {} Body: {}'.format(part, part.body)
Content-Type: (text/plain) Body: Hello, Alice Content-Type: (text/html) Body:
Hello, Alice
None because no enclosed messages exist
print msg.enclosed None