4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3のDebuggingServerログの変換(バイナリ⇨UTF8)

Last updated at Posted at 2018-05-12

前提

Pythonでメール送信の開発時のチェックとして

python -m smtpd -n -c DebuggingServer localhost:1025

(ローカルホスト、ポート1025)
を利用しているが、バイナリ表記でコンソールに表示される
修正方法が見つからなかったので、対応した内容を投稿しておきます。

対応方法

以下のファイルを適当な場所に[smtp.py]というファイル名で作成

smtp.py

# coding: utf-8

import smtpd


class DebuggingServer(smtpd.DebuggingServer):
    def _print_message_content(self, peer, data):
        inheaders = 1
        lines = data.splitlines()
        for line in lines:
            # headers first
            if inheaders and not line:
                peerheader = 'X-Peer: ' + peer[0]
                if not isinstance(data, str):
                    # decoded_data=false; make header match other binary output
                    peerheader = repr(peerheader.encode('utf-8'))
                print(peerheader)
                inheaders = 0
            if not isinstance(data, str):
                # Avoid spurious 'str on bytes instance' warning.
                line = repr(line.decode('utf-8'))
            print(line)
python -m smtpd -n -c smtp.DebuggingServer localhost:1025

で起動することでUTF-8の形式で出力される。

補足

元の実装から

line = repr(line)


以下にオーバライドしている

line = repr(line.decode('utf-8'))
4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?