如何让服务器侦听多个端口

时间:2014-08-05 09:48:54

标签: python sockets

我想在同一台服务器上侦听100个不同的TCP端口。这就是我目前正在做的事情: -

import socket
import select

def main():

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    for i in range(1000,1100):
        server_socket.bind(('127.0.0.1', i))
    server_socket.listen(1)

    read_list = [server_socket]
    while True:
        readable, writable, exceptional = select.select(read_list, [], read_list)
        for s in readable:
            if s is server_socket:
                #print "client connected"
                client_socket, address = server_socket.accept()
                read_list.append(client_socket)
            else:
                # One of the tcp clients
                data = s.recv(1024)
                if not result:
                    s.close()
                    read_list.remove(s)
                    #print "client disconnected"

if __name__ == "__main__":
    main()

它返回错误Permission Denied。是因为某些端口(1000-1100)是保留的并且未分配给它或由于其他原因?

我尝试过(8000-8100)并且说Invalid Arguments

EDITED

import socket
import select

def create_socket(TCP_PORT):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(('127.0.0.1', TCP_PORT))
    server_socket.listen(1)

    return server_socket


def main():

    read_list = []

    for TCP_PORT in range(8000,8100):
        read_list.append(create_socket(TCP_PORT))

    while True:
        readable, writable, exceptional = select.select(read_list, [], read_list)
        for s in readable:
            if s is server_socket:
                #print "client connected"
                client_socket, address = server_socket.accept()
                read_list.append(client_socket)
            else:
                # One of the tcp clients
                data = s.recv(1024)
                if not result:
                    s.close()
                    read_list.remove(s)
                    #print "client disconnected"

if __name__ == "__main__":
    main()

3 个答案:

答案 0 :(得分:5)

有两个问题。

  1. 保留端口below 1024。 (您需要特殊权限,例如绑定到此类端口的root权限。)

  2. 套接字只能侦听一个端口。因此,要监听多个端口,您需要为每个端口创建一个套接字。

答案 1 :(得分:1)

你会在这里找到一个很好的解释:Listen to multiple ports from one server。它适用于C但python中的问题是相同的。

所以答案是一样的:

  • 每个端口一个插槽
  • 每个插座听一次
  • 一次选择

顺便说一下,在Unix(和类Unix)系统上保留1024以下的端口:您需要root权限才能使用它们。在Windows上,没有这样的限制。

答案 2 :(得分:0)

关于第二个问题 - 使用端口8000-8100导致Invalid argument,如果您尝试重新绑定已绑定的套接字而不首先重新创建套接字,则会发生这种情况。但是,该端口范围没有问题。

>>> s=socket.socket()
>>> s.bind(('localhost', 8001))
>>> s.bind(('localhost', 8001))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument
>>> s.close()
>>> s.bind(('localhost', 8001))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
  File "/usr/lib64/python2.7/socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
>>> s=socket.socket()
>>> s.bind(('localhost', 8001))