开发者

How do I set up a localhost in python?

The project is to be able to send email from within Python. I can successfully send to, for example, "smtp.gmail.com", but using a localhost or 127.0.0.1 returns an "errno 111, connection was refused." The statement is

    server = smtplib.SMTP('127.0.0.1', 8025)

Notes: I am running Ubuntu, Python 3.2 or Python 2.7 depending on what the wonderful people here know best, and my programming k开发者_如何学运维nowledge could be charitably described as limited.


First, test your local SMTP connection using Telnet. (Alternate instructions here) This will tell if you can connect to your local SMTP server in the way you think you can.

You probably have one of several problems:

  1. No SMTP server on your machine or;
  2. You have SMTP but it requires a password and/or;
  3. You have SMTP on your local machine but it is not configured properly or configured to support SMTP locally -> Internet or;
  4. The machine's current IP or DNS setup or;
  5. The way you are calling the local SMTP server from Python.

Python SMTPlib supports authorized connections using LMTP for issue 2.

Try using port 25 for issue 5.

For the other issues, you will need to find or setup a cooperative SMTP server.

BTW: properly configuring and securing an SMTP server is not trivial...


What you're attempting to do, is using the local machine as the smtp-server. The simplest way to achieve this in Ubuntu, would be to install one of the exim4 or postfix-packages in apt / synaptic ...

Make sure that when prompted, that you don't allow machines you don't trust to use your host as a mail-relay. The defaults should be sufficient to avoid this - yet still allow mail to be sent from your machine.

Also note that the default port-number for smtp is port 25, and not 8025 as in your code example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜