Python socket.error: [Errno 13] 创建假电子邮件服务器时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25709716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:32:15  来源:igfitidea点击:

socket.error: [Errno 13] Permission denied when creating a fake email server

pythonsocketsflask

提问by Takeshi Patterson

I'm trying to create a fake email server as part of a Flask app to print out errors on the console by using the following script. However, it throws an error. How can I fix this?

我正在尝试创建一个假电子邮件服务器作为 Flask 应用程序的一部分,以使用以下脚本在控制台上打印错误。但是,它会引发错误。我怎样才能解决这个问题?

dpadmins-MacBook:microblog presentation$ python -m smtpd -n -c DebuggingServer localhost:25

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162,     in _run_module_as_main
"__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
  exec code in run_globals
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py", line 536,  in <module>
  (options.remotehost, options.remoteport))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py", line 285, in __init__
 self.bind(localaddr)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py", line 342, in bind
 return self.socket.bind(addr)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
 return getattr(self._sock,name)(*args)
socket.error: [Errno 13] Permission denied

采纳答案by solarnz

In unix (Linux, Mac OS X, BSD etc) systems, ports less than 1024 can not be bound to by normal users, only the root user can bind to those ports.

在 unix(Linux、Mac OS X、BSD 等)系统中,普通用户无法绑定小于 1024 的端口,只有 root 用户可以绑定这些端口。

To get around this, you could run your python application as root (by using sudo), however this is not preferable. Is it possible instead to get your Flask application to talk to localhost on a higher port, say 2525? You would then need to modify the command you are using to start the smtp server to bind on port 2525 rather than 25.

为了解决这个问题,您可以以 root 身份运行您的 python 应用程序(通过使用 sudo),但这不是可取的。是否有可能让您的 Flask 应用程序在更高的端口上与本地主机通信,比如 2525?然后,您需要修改用于启动 smtp 服务器的命令以绑定端口 2525 而不是 25。

回答by Daniel

Run the program as superuser. The smtp-port as any port <=1024 is reserved to the system and cannot be used by normal users.

以超级用户身份运行程序。smtp-port as any port <=1024 是系统保留的,普通用户不能使用。

回答by Burhan Khalid

If you are doing this as an exercise, then @solarnz has the right approach. If however, you need this done for work there is a far better solution in mailcatcher:

如果您将此作为练习,那么@solarnz 有正确的方法。但是,如果您需要完成这项工作,那么在mailcatcher 中有一个更好的解决方案:

MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://127.0.0.1:1025instead of your default SMTP server, then check out http://127.0.0.1:1080to see the mail that's arrived so far.

MailCatcher 运行一个超级简单的 SMTP 服务器,它捕获发送给它的任何消息以显示在 Web 界面中。运行 mailcatcher,设置您最喜欢的应用程序smtp://127.0.0.1:1025而不是默认的 SMTP 服务器,然后查看http://127.0.0.1:1080目前已到达的邮件。

This is a program designed especially for developers whose apps need a mail server for testing but they don't want to set one up.

这是一个专为应用程序需要邮件服务器进行测试但又不想设置邮件服务器的开发人员设计的程序。

The great bonus is that it comes with a web interface to view messages sent by your application:

最大的好处是它带有一个 Web 界面来查看您的应用程序发送的消息:

mailcatcher web interface

邮件捕手网络界面

回答by Harsh Daftary

Execute program with root or sudo previliages, but as suggested above this is not recommended,

使用 root 或 sudo previliages 执行程序,但如上所述,不推荐这样做,

so setup your service on port >= 1024 and then setup reverse proxy for your service, or

所以在端口 >= 1024 上设置您的服务,然后为您的服务设置反向代理,或者

redirect all traffice from port 25 to your service port,

将所有流量从端口 25 重定向到您的服务端口,

For example :

例如 :

/sbin/iptables -t nat -I PREROUTING -p tcp --dport 25 -j REDIRECT --to-port 2525

回答by Hyo Da Seo

maybe SELinux cause this problem.. I solved problem through by "setenforce 0".

也许 SELinux 导致了这个问题。我通过“setenforce 0”解决了问题。