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.send_response(200) self.send_header("Content-Length",len(message)) self.end_headers() self.wfile.write(bytes(message, "utf8")) return server = ('localhost', 80) httpd = HTTPServer(server, RequestHandler) httpd.serve_forever()
The output message is
PermissionError: [Errno 13] Permission denied
Advertisement
Answer
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) httpd.serve_forever()