簡體   English   中英

Python3 Sockets - PermissionError: [Errno 1] Operation not allowed - 作為 root 運行

[英]Python3 Sockets - PermissionError: [Errno 1] Operation not permitted - running as root

我很難使用我擁有的這個小型套接字程序。 該腳本在 Debian 11 服務器上運行良好,但在幾乎相同的 Debian 11 服務器上出現以下錯誤。

root@box:/home/user/python# sudo python3 script.py
Traceback (most recent call last):
  File "/home/user/python/script.py", line 18, in <module>
    UDPClientSocket.sendto(bytesToSend, serverAddress) # Send using created UDP socket
PermissionError: [Errno 1] Operation not permitted

該文件由 root 擁有並且可執行。

-rwxr-xr-x 1 root root 942 May 27 16:24 script.py

這是腳本,它非常基本。

import socket
from Cryptodome.Cipher import AES

serverAddress   = ("192.168.1.3", 3493)  #Packet Destination - IP/Port pair
sourceAddress   = ("192.168.3.2", 54921) #Source Socket - IP/Port pair
bufferSize      = 1024

data=b"garbage"          # message to send to server
key=b"xxxxxxxxxxxxxxxx"                           # key to encrypt data
salt=b'xxxxxxxxxxxxxxxx'                        # salt
obj=AES.new(key, AES.MODE_CBC, salt)            # set crypt settings
bytesToSend=obj.encrypt(data)                   # encrypt the data

UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) # Create a UDP socket at client side
UDPClientSocket.bind((sourceAddress)) # Set local socket for source ip/port
UDPClientSocket.sendto(bytesToSend, serverAddress) # Send using created UDP socket

我以 root 身份運行,腳本歸 root 所有,這里有什么問題?

我在腳本中做的事情是操作系統不允許的嗎?

更新:

為它運行的機器命名: Linux box 5.10.0-14-amd64 #1 SMP Debian 5.10.113-1 (2022-04-29) x86_64 GNU/Linux

未在其上運行的機器的名稱: Linux box 5.10.0-13-amd64 #1 SMP Debian 5.10.106-1 (2022-03-17) x86_64 GNU/Linux

Python 版本相同,都是 3.9.2。

我嘗試在其上運行腳本的框的界面列表:

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: net1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 04:d9:f5:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    altname enp2s0
    inet 192.168.3.2/28 brd 192.168.3.15 scope global net1
       valid_lft forever preferred_lft forever

我想通了,我的錯誤。

原來是防火牆。 我有帶有輸出過濾器的 iptables,並且在上次重新啟動之前忘記了 iptables-save,因此允許 UDP sendto 的輸出規則被拒絕。

當我在 udp 模式下嘗試 ncat 時發現它並因WRITE ERROR失敗

我感謝大家的意見。

運行此更新


pip install --upgrade setuptools pip

再試一次

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM