PermissionError: [Errno 13] Permission denied on mac

When I try to run the code below, the mac refuse the connection.

from http.server import BaseHTTPRequestHandler, HTTPServer
class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        message = "Welcome to COE 550!"
        self.protocol_version = "HTTP/1.1"
        self.wfile.write(bytes(message, "utf8"))

server = ('localhost', 80)
httpd = HTTPServer(server, RequestHandler)

The output message is

PermissionError: [Errno 13] Permission denied

>Solution :

The port 80 is considered as privileged port(TCP/IP port numbers below 1024) so the process using them must be owned by root. When you run a server as a test from a non-priviliged account, you have to test it on other ports, such as 2784, 5000, 8001 or 8080.

You could either run the python process as root or you have to use any non privileged port to fix this issue.

server = ('localhost', 8001)
httpd = HTTPServer(server, RequestHandler)

Leave a Reply