简体   繁体   English

Linux [Errno 13] 权限被拒绝

[英]Linux [Errno 13] Permission denied

I have a python script that works on Windows but am having a hard time running it on Ubuntu Desktop.我有一个 python 脚本可以在 Windows 上运行,但很难在 Ubuntu 桌面上运行它。 Not a lot of wisdom here any tips greatly appreciated...这里没有很多智慧,任何提示都非常感谢......

test.py测试.py

#!/usr/bin/env python3

"""
https://pymodbustcp.readthedocs.io/en/latest/examples/server_allow.html

An example of Modbus/TCP server which allow modbus read and/or write only from
specific IPs.

Run this as root to listen on TCP privileged ports (<= 1024).
"""

import argparse
from pyModbusTCP.server import ModbusServer, DataHandler
from pyModbusTCP.constants import EXP_ILLEGAL_FUNCTION


# some const
ALLOW_R_L = ['127.0.0.1', '192.168.0.104']
ALLOW_W_L = ['127.0.0.1']


# a custom data handler with IPs filter
class MyDataHandler(DataHandler):
    def read_coils(self, address, count, srv_info):
        if srv_info.client.address in ALLOW_R_L:
            return super().read_coils(address, count, srv_info)
        else:
            return DataHandler.Return(exp_code=EXP_ILLEGAL_FUNCTION)

    def read_d_inputs(self, address, count, srv_info):
        if srv_info.client.address in ALLOW_R_L:
            return super().read_d_inputs(address, count, srv_info)
        else:
            return DataHandler.Return(exp_code=EXP_ILLEGAL_FUNCTION)

    def read_h_regs(self, address, count, srv_info):
        if srv_info.client.address in ALLOW_R_L:
            return super().read_h_regs(address, count, srv_info)
        else:
            return DataHandler.Return(exp_code=EXP_ILLEGAL_FUNCTION)

    def read_i_regs(self, address, count, srv_info):
        if srv_info.client.address in ALLOW_R_L:
            return super().read_i_regs(address, count, srv_info)
        else:
            return DataHandler.Return(exp_code=EXP_ILLEGAL_FUNCTION)

    def write_coils(self, address, bits_l, srv_info):
        if srv_info.client.address in ALLOW_W_L:
            return super().write_coils(address, bits_l, srv_info)
        else:
            return DataHandler.Return(exp_code=EXP_ILLEGAL_FUNCTION)

    def write_h_regs(self, address, words_l, srv_info):
        if srv_info.client.address in ALLOW_W_L:
            return super().write_h_regs(address, words_l, srv_info)
        else:
            return DataHandler.Return(exp_code=EXP_ILLEGAL_FUNCTION)


if __name__ == '__main__':
    # parse args
    parser = argparse.ArgumentParser()
    parser.add_argument('-H', '--host', type=str, default='localhost', help='Host (default: localhost)')
    parser.add_argument('-p', '--port', type=int, default=502, help='TCP port (default: 502)')
    args = parser.parse_args()
    # init modbus server and start it
    server = ModbusServer(host=args.host, port=args.port, data_hdl=MyDataHandler())
    server.start()

When I run this from SSH I get:当我从 SSH 运行它时,我得到:

Traceback (most recent call last):
  File "/home/ben/anaconda3/lib/python3.7/site-packages/pyModbusTCP/server.py", line 989, in start
    self._service.server_bind()
  File "/home/ben/anaconda3/lib/python3.7/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
PermissionError: [Errno 13] Permission denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 69, in <module>
    server.start()
  File "/home/ben/anaconda3/lib/python3.7/site-packages/pyModbusTCP/server.py", line 992, in start
    raise ModbusServer.NetworkError(e)
pyModbusTCP.server.NetworkError: [Errno 13] Permission denied

If I do a ls -la on the directory:如果我在目录上执行ls -la

total 12
drwxrwxr-x 2 ben ben 4096 Aug  7 10:57 .
drwxr-xr-x 5 ben ben 4096 Aug  7 10:50 ..
-rw-rw-r-- 1 ben ben 2522 Aug  7 10:54 test.py

In Unix based environments (Linux, etc.) TCP ports of 1024 and below are considered privileged.在基于 Unix 的环境(Linux 等)中,1024 及以下的 TCP 端口被视为特权。 There are historical reasons for this as internal services run on low ports (ie sshd on port 22).这有历史原因,因为内部服务在低端口上运行(即 sshd 在端口 22 上)。

You have two ways to fix this:您有两种方法可以解决此问题:

  1. Run as root to allow you to bind to port 502 which appears to be the default.以 root 身份运行以允许您bind到默认端口 502。
  2. Run with the -p or --port command line option to specify a port of 1025 or more.使用-p--port命令行选项运行以指定 1025 或更大的端口。 These ports do not require root privileges.这些端口不需要 root 权限。

What user are you running the script with?您正在使用哪个用户运行脚本?

In order not to change the user to root, you could test by typing sudo at the beginning of the command to run the script.为了不将用户更改为 root,您可以通过在命令开头键入 sudo 来运行脚本来进行测试。

Example

sudo python3 script.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM