首页 > 解决方案 > PermissionError:[Errno 13] 运行以 Python 编码的服务器端 CGI 脚本时权限被拒绝

问题描述

我正在尝试运行用 python 编码的服务器端 cgi 脚本,但在运行它时出现以下错误。

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

这是我要运行的脚本。

"""
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()

任何帮助将不胜感激。提前致谢。

标签: python-3.xserver-side-scripting

解决方案


用这个

chmod 777 script_name,

然后执行脚本,如果它再次失败然后使用

sudo python script_name

或检查如何在您各自的操作系统上使用管理员权限运行脚本。


推荐阅读