0

I have a Flask app that is being run with Tornado:

...some imports

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

...some code

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()

In order to view the app, I go to

http://127.0.0.1:5000  

However, I want to be able to send email with the same app:

  print "Launch server"
  s = smtplib.SMTP(host='127.0.0.1', port='5000')
  print "Send mail"
  s.sendmail(me, [you], msg.as_string())
  s.quit()

When the time comes to send the email, the app just hangs instead of sending the mail. If I do this:

s = smtplib.SMTP('localhost')

I get an error message. Can someone help me out?

Thanks.

UPDATE:

When I use:

s = smtplib.SMTP('localhost')

I get this error message:

ERROR:tornado.application:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/tornado/iostream.py", line 354, in wrapper
    callback(*args)
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/tornado/stack_context.py", line 331, in wrapped
    raise_exc_info(exc)
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/tornado/stack_context.py", line 302, in wrapped
    ret = fn(*args, **kwargs)
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/tornado/httpserver.py", line 328, in _on_headers
    self.request_callback(self._request)
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/tornado/wsgi.py", line 253, in __call__
    response.extend(app_response)
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/werkzeug/wsgi.py", line 682, in __next__
    return self._next()
  File "/Users/christopherspears/.virtualenvs/traffic/lib/python2.7/site-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
    for item in iterable:
  File "traffic.py", line 157, in event_stream
    detect_spike(row, threshold, me, you)
  File "traffic.py", line 132, in detect_spike
    s = smtplib.SMTP('localhost')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
    (code, msg) = self.connect(host, port)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 310, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 285, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 61] Connection refused

Maybe tornado and smtp are not playing nice?

Christopher Spears
  • 1,065
  • 15
  • 31
  • What is the error message? – Ben Darnell Apr 19 '14 at 15:17
  • 1
    Are you sure you're running an smtp server locally (and on the correct port)? The error message indicates that there is no smtp server running on localhost (or that it's been firewalled off). The earlier code suggests that you're trying to use port 5000 for both smtp and http, which won't work (although it's not clear how the different code fragments in this message are meant to fit together) – Ben Darnell Apr 19 '14 at 18:33
  • I ended up just using Gmail to send the email: http://stackoverflow.com/questions/10147455/trying-to-send-email-gmail-as-mail-provider-using-python – Christopher Spears Apr 21 '14 at 02:24

0 Answers0