Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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
like image 495
Takeshi Patterson Avatar asked Sep 07 '14 11:09

Takeshi Patterson


People also ask

How do I fix errno 13 Permission denied?

We can solve this error by Providing the right permissions to the file using chown or chmod commands and also ensuring Python is running in the elevated mode permission .

How do I fix permission is denied in Python?

Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it. It worked for me.

Can not open server socket for SMTP?

The “could not open socket” means POST SMTP cannot talk to your mail server. It could also be that your mail server is closing the connection, in which case you should see something in the transcription logs. Have you run the diagnostic/connectivity test?

What is error number 13 in Python?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.


2 Answers

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.

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.

like image 168
solarnz Avatar answered Oct 26 '22 13:10

solarnz


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:

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:1025 instead of your default SMTP server, then check out http://127.0.0.1:1080 to see the mail that's arrived so far.

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:

mailcatcher web interface

like image 24
Burhan Khalid Avatar answered Oct 26 '22 13:10

Burhan Khalid