在localhost中,如何选择一个空闲端口号?

时间:2022-11-17 18:13:41

I'm trying to play with inter-process communication and since I could not figure out how to use named pipes under Windows I thought I'll use network sockets. Everything happens locally. The server is able to launch slaves in a separate process and listens on some port. The slaves do their work and submit the result to the master. How do I figure out which port is available? I assume I cannot listen on port 80 or 21?

我正在尝试进行进程间通信,由于我无法在Windows下使用命名管道,所以我认为我将使用网络套接字。在本地发生的一切。服务器能够在单独的进程中启动从服务器,并监听某些端口。奴隶们做他们的工作,把结果交给主人。如何确定哪个端口可用?我想我听不懂端口80还是21?

I'm using Python, if that cuts the choices down.

我用的是Python,如果这能降低选项。

Thanks!

谢谢!

4 个解决方案

#1


178  

Do not bind to a specific port, or bind to port 0, e.g. sock.bind(('', 0)). The OS will then pick an available port for you. You can get the port that was chosen using sock.getsockname()[1], and pass it on to the slaves so that they can connect back.

不要绑定到特定的端口,或绑定到端口0,例如袜子。绑定(0)(“)。操作系统将为您选择一个可用的端口。您可以使用sock.getsockname()[1]获取所选的端口,并将其传递给从服务器,以便它们能够连接回来。

#2


37  

Bind the socket to port 0. A random free port from 1024 to 65535 will be selected. You may retrieve the selected port with getsockname() right after bind().

将套接字绑定到端口0。将选择一个从1024到65535的随机*端口。您可以在bind()之后使用getsockname()检索所选的端口。

#3


10  

For the sake of snippet of what the guys have explained above:

为了上面那些人解释的片段:

import socket
from contextlib import closing

def find_free_port():
    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
        s.bind(('', 0))
        return s.getsockname()[1]

#4


2  

You can listen on whatever port you want; generally, user applications should listen to ports 1024 and above (through 65535). The main thing if you have a variable number of listeners is to allocate a range to your app - say 20000-21000, and CATCH EXCEPTIONS. That is how you will know if a port is unusable (used by another process, in other words) on your computer.

你可以在任何你想要的港口听;通常,用户应用程序应该监听端口1024和以上(通过65535)。如果侦听器的数量是可变的,那么最重要的是为应用程序分配范围——比如20000-21000,并捕获异常。这就是您如何知道一个端口是否不可用(在另一个进程中使用,换句话说)在您的计算机上。

However, in your case, you shouldn't have a problem using a single hard-coded port for your listener, as long as you print an error message if the bind fails.

但是,在您的示例中,只要在绑定失败时打印错误消息,就不需要为侦听器使用单个硬编码端口。

Note also that most of your sockets (for the slaves) do not need to be explicitly bound to specific port numbers - only sockets that wait for incoming connections (like your master here) will need to be made a listener and bound to a port. If a port is not specified for a socket before it is used, the OS will assign a useable port to the socket. When the master wants to respond to a slave that sends it data, the address of the sender is accessible when the listener receives data.

还要注意,您的大多数套接字(对于从属字)不需要显式地绑定到特定的端口号——只有等待传入连接(如这里的主接字)的套接字需要成为侦听器并绑定到端口。如果在使用套接字之前没有为套接字指定端口,操作系统将为套接字分配一个可用的端口。当主服务器希望响应发送数据的从服务器时,侦听器接收数据时可以访问发送方的地址。

I presume you will be using UDP for this?

我想你会用UDP做这个?

#1


178  

Do not bind to a specific port, or bind to port 0, e.g. sock.bind(('', 0)). The OS will then pick an available port for you. You can get the port that was chosen using sock.getsockname()[1], and pass it on to the slaves so that they can connect back.

不要绑定到特定的端口,或绑定到端口0,例如袜子。绑定(0)(“)。操作系统将为您选择一个可用的端口。您可以使用sock.getsockname()[1]获取所选的端口,并将其传递给从服务器,以便它们能够连接回来。

#2


37  

Bind the socket to port 0. A random free port from 1024 to 65535 will be selected. You may retrieve the selected port with getsockname() right after bind().

将套接字绑定到端口0。将选择一个从1024到65535的随机*端口。您可以在bind()之后使用getsockname()检索所选的端口。

#3


10  

For the sake of snippet of what the guys have explained above:

为了上面那些人解释的片段:

import socket
from contextlib import closing

def find_free_port():
    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
        s.bind(('', 0))
        return s.getsockname()[1]

#4


2  

You can listen on whatever port you want; generally, user applications should listen to ports 1024 and above (through 65535). The main thing if you have a variable number of listeners is to allocate a range to your app - say 20000-21000, and CATCH EXCEPTIONS. That is how you will know if a port is unusable (used by another process, in other words) on your computer.

你可以在任何你想要的港口听;通常,用户应用程序应该监听端口1024和以上(通过65535)。如果侦听器的数量是可变的,那么最重要的是为应用程序分配范围——比如20000-21000,并捕获异常。这就是您如何知道一个端口是否不可用(在另一个进程中使用,换句话说)在您的计算机上。

However, in your case, you shouldn't have a problem using a single hard-coded port for your listener, as long as you print an error message if the bind fails.

但是,在您的示例中,只要在绑定失败时打印错误消息,就不需要为侦听器使用单个硬编码端口。

Note also that most of your sockets (for the slaves) do not need to be explicitly bound to specific port numbers - only sockets that wait for incoming connections (like your master here) will need to be made a listener and bound to a port. If a port is not specified for a socket before it is used, the OS will assign a useable port to the socket. When the master wants to respond to a slave that sends it data, the address of the sender is accessible when the listener receives data.

还要注意,您的大多数套接字(对于从属字)不需要显式地绑定到特定的端口号——只有等待传入连接(如这里的主接字)的套接字需要成为侦听器并绑定到端口。如果在使用套接字之前没有为套接字指定端口,操作系统将为套接字分配一个可用的端口。当主服务器希望响应发送数据的从服务器时,侦听器接收数据时可以访问发送方的地址。

I presume you will be using UDP for this?

我想你会用UDP做这个?