Socket.error: [Errno 13] Permission denied when creating a fake mail server

I am trying to create a fake mail server as part of a Flask application to print errors to the console using the following script. However, it causes an error. How can i fix this?

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 
+5
source share
5 answers

On Unix systems (Linux, Mac OS X, BSD, etc.) ports that are smaller than 1024 cannot bind to regular users, only the root user can bind to these ports.

To get around this, you can run the python application as root (using sudo), however this is not preferred. Is it possible instead to make the Flask application talk to localhost on a higher port, say 2525? Then you will need to change the command you use to start the SMTP server to bind to port 2525 instead of 25.

+22
source

If you do this as an exercise, then @solarnz has the right approach. If, however, you need to do this to work, there is a much better solution in mailcatcher :

MailCatcher launches a super simple SMTP server that catches any message sent to it for display in the web interface. Launch your mailbox, install your favorite application to deliver smtp://127.0.0.1:1025 instead of your default SMTP server, then check http://127.0.0.1:1080 to see which has arrived so far.

This is a program designed specifically for developers whose applications need a mail server for testing, but they do not want to install it.

A great bonus is that it comes with a web interface for viewing messages sent by your application:

mailcatcher web interface

+3
source

Run the program using root or sudo previliages, but as suggested above, this is not recommended.

so configure the service on port> = 1024 and then configure the reverse proxy for your service or

redirect all traffic from port 25 to your service port,

For instance:

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

Run the program as superuser. The smtp port, like any port <= 1024, is reserved for the system and cannot be used by ordinary users.

0
source

perhaps SELinux is causing this problem. I solved the problem through "setenforce 0".

0
source

Source: https://habr.com/ru/post/1202009/


All Articles