python-3.xserver-side-scripting

PermissionError: [Errno 13] Permission denied while running a server-side CGI scripts coded in Python


I am trying to run a server-side cgi script coded in python but I am getting the following error while running it.

Traceback (most recent call last):
  File "webserver.py", line 16, in <module>
    srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 449, in __init__
    self.server_bind()
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/server.py", line 137, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 463, in server_bind
    self.socket.bind(self.server_address)
PermissionError: [Errno 13] Permission denied

Here is the script that I am trying to run.

"""
Implement an HTTP web server in Python that knows how to run server-side
CGI scripts coded in Python; serves files and scripts from current working
dir; Python scripts must be stored in webdir\cgi-bin or webdir\htbin;

"""

import os,sys
from http.server import HTTPServer, CGIHTTPRequestHandler

webdir = '.'
port = 80       #default http://localhost/, else use http://localhost:xxxx/

os.chdir(webdir)
srvraddr  = ("",port)
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
srvrobj.serve_forever()

Any help will be appreciated. Thanks in advance.


Solution

  • Use this

    chmod 777 script_name,

    then execute the script if it fails again then use

    sudo python script_name

    or check how to run script using administrator privileges on your respective operating system.