python bind socket.error: [Errno 13] 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24001147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:48:36  来源:igfitidea点击:

python bind socket.error: [Errno 13] Permission denied

pythontunnel

提问by NELOUNI

I have a python script which gets packets from a remote machine and writes them (os.write(self.tun_fd.fileno(), ''.join(packet))) to a tun interface gr3:

我有一个 python 脚本,它从远程机器获取数据包并将它们 (os.write(self.tun_fd.fileno(), ''.join(packet))) 写入 tun 接口 gr3:

Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
inet addr:10.0.0.6  P-t-P:10.0.0.8  Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
RX packets:61 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500 
RX bytes:5124 (5.0 KiB)  TX bytes:0 (0.0 b)

I would like to receive those packets via a separate pong script as follows:

我想通过一个单独的 pong 脚本接收这些数据包,如下所示:

import threading, os, sys, fcntl, struct, socket
from fcntl import ioctl
from packet import Packet

HOST = '10.0.0.6'
PORT = 111
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    else: print data    
    conn.sendall(data)
conn.close()

I got this error :

我收到此错误:

s.bind((HOST, PORT))
File "<string>", line 1, in bind
socket.error: [Errno 13] Permission denied

采纳答案by utdemir

You can't bind to port numbers lower than 1024 as a unprivileged user.

作为非特权用户,您不能绑定到低于 1024 的端口号。

So you should either:

所以你应该:

  • Use a port number larger than 1024 (recommended)
  • Or run the script as a privileged user
  • 使用大于 1024 的端口号(推荐)
  • 或者以特权用户身份运行脚本

Harder, but more secure solution if it's really necessary to accept from 111:

如果确实有必要从 111 接受,则更难但更安全的解决方案:

  • Run the as unprivileged on a higher port, and forward port 111 to it externally.
  • 在更高的端口上以非特权身份运行,并将端口 111 转发到外部。