简体   繁体   中英

Byte/string mismatch in aiosmtpd.handlers.Proxy.handle_DATA

I'm new with asyncio / aiosmtpd. Am I using this wrong, or is this a bug?

Running the code below, I get an error indicating that we're confusing bytes with strings. I run this with Python 3.6.1 on Linux with aiosmtpd 1.0a5.

#!/usr/bin/env python3
import asyncio
from aiosmtpd.controller import Controller
from aiosmtpd.handlers import Proxy
import logging


async def proxy_mail(loop):
    cont = Controller(
        Proxy('localhost', 8025),
        hostname='localhost',
        port=1025)
    cont.start()


def main():
    logging.basicConfig(level=logging.DEBUG)
    loop = asyncio.get_event_loop()
    loop.create_task(proxy_mail(loop=loop))
    loop.run_forever()


if __name__ == '__main__':
    main()

I get this traceback.

Traceback (most recent call last):
  File "/home/magnusl/.pyenv/versions/3.6.1/lib/python3.6/site-packages/aiosmtpd/smtp.py", line 235, in _handle_client
    yield from method(arg)
  File "/home/magnusl/.pyenv/versions/3.6.1/lib/python3.6/site-packages/aiosmtpd/smtp.py", line 585, in smtp_DATA
    status = yield from self._call_handler_hook('DATA')
  File "/home/magnusl/.pyenv/versions/3.6.1/lib/python3.6/site-packages/aiosmtpd/smtp.py", line 187, in _call_handler_hook
    status = yield from hook(self, self.session, self.envelope, *args)
  File "/home/magnusl/.pyenv/versions/3.6.1/lib/python3.6/asyncio/coroutines.py", line 210, in coro
    res = func(*args, **kw)
  File "/home/magnusl/.pyenv/versions/3.6.1/lib/python3.6/site-packages/aiosmtpd/handlers.py", line 101, in handle_DATA
    if NLCRE.match(line):
TypeError: cannot use a string pattern on a bytes-like object

If I change this in the Proxy class in aiosmtpd/handlers.py:

@asyncio.coroutine
def handle_DATA(self, server, session, envelope):
    lines = envelope.content.splitlines(keepends=True)

to this

@asyncio.coroutine
def handle_DATA(self, server, session, envelope):
    lines = str(envelope.content).splitlines(keepends=True)

it seems to work, but I don't know enough about the envelope, mail protocols, encodings etc to know whether this is an appropriate fix, or even if I'm using the library right. It looks to me like this handler could never work, and I assumed there were some tests...

Indeed, this is a bug in aiosmtpd 1.0a5 (also reported in this question ). It was reported and subsequently fixed ( v1 , v2 ).

aiosmtpd 1.0b1 was recently released with this fix. Please try upgrading -- I tried your script on both versions and got a failure on 1.0a5 and a success on 1.0b1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM