检查socket端口是否可用 [英] Checking whether socket port is available

查看:91
本文介绍了检查socket端口是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我有一个应用程序,它有一个 TCP 服务器(基于 socketserver),我想测试它的功能.

I have an application, which has a TCP server as its part (based on socketserver), and I wanna test it functionally.

我为自己创建了一个测试 mixin,我在每个测试用例中都使用它:

I've created myself a testing mixin, which I use in every test case:

class AppProcessManagingMixin:
    _used_tcp_ports = set()

    def start_my_app(self):
        port = self._take_tcp_port()
        prepare_config_with_given_tcp_port(port)
        # use prepared config by subprocess below
        Popen([python_executable_path, my_app_name, ...).start()

    def _take_tcp_port(self):
        available_found = False
        while not available_found:
            port = random.randint(9000, 9999) # these ints are arbitrary here
            available_found = port not in self._used_tcp_ports
        self._used_tcp_ports.add(port)
        return port

    def stop_my_app(self):
        so_something_that_Im_sure_will_stop_my_app_after_a_few_seconds()

每个测试用例 setUp() 调用 self.start_my_app(),并且每个 tearDown() - self.stop_my_app(),我可以依靠停止我的应用程序.我需要我的应用程序的许多实例(并且无法在类范围的设置/拆卸中启动/停止应用程序),因为我的一些测试正在检查应用程序是否在以空工作目录开始的场景中运行良好(我只需要空"应用程序实例对于那些测试).

Every test case setUp() calls self.start_my_app(), and every tearDown() - self.stop_my_app(), and I can rely on stopping my app. I need many instances of my app (and cannot start/stop app in class-wide setup/teardown), because some of my test are checking whether app works well in scenarios starting with empty working directory (I just need "empty" app instance for those tests).

我对第二种方法有问题,_take_tcp_port.在我介绍它之前,我经常遇到已取地址的问题(因为所有测试都尝试在同一端口上运行 TCP 服务器).现在它不经常发生了,但仍然 - 它发生了.

I have a problem with second method, _take_tcp_port. Before I introduced it, I often had problems with already taken address (because all tests tried running TCP server on the same port). Now it doesn't happen so often, but still - it happens.

我知道它依赖于系统,几秒钟后端口将再次可用,但它使自动化测试变得更加困难.

I know that it is system-dependent, and after few seconds port will be available again, but it makes automated testing harder.

问题:

如何检查某个地址(主机和端口)是否可用于 TCP 服务器?我想在 _take_tcp_port 中检查它,以确保测试不会因为应用程序实例的端口问题而失败.

How do I check, whether some address (host and port) is available for TCP server? I wanna check it in _take_tcp_port, to be sure that test won't fail because app instance has problems with taken port.

我做过的研究:

我浏览了以下谷歌搜索的第一页:

I've browsed 1st pages of following google searches:

  • 检查端口是否打开python
  • 检查端口是否可用python
  • 发现可用端口

以及我找到的所有方法都基于尝试获取套接字.事实是,如果我成功获取套接字,我可以确定这个端口是打开的,但是因为我这样做了,我不能确定我的应用程序是否能够获取它.我需要没有副作用的东西.

and all approaches I've found base on trying to obtain socket. Thing is, if I obtain socket successfully I can be sure that this port WAS open, but because I did it, I cannot be sure that my app will be able to obtain it. I need something without side effects.

平台

这需要是多平台的(至少是 Windows、Linux、MacOS).它必须至少在 p3.3+ 中工作.如果有一个库"就足够了;)

This needs to be multiplatform (at least Windows, Linux, MacOS). It must work at least in p3.3+. If "there's a lib for that" that will be enough ;)

推荐答案

您无需手动扫描可用端口——操作系统会为您完成.调用 bind() 时只需指定端口 0,操作系统就会选择一个.这通常对测试很有用——您可以使用端口 0 启动一个端点,让它询问系统它实际获得的端口是什么 (getsockaddr),然后使用该端口号启动第二个端点.

You don't need to manually scan for an available port--the OS will do it for you. Just specify port 0 when you call bind() and the OS will choose one. This is often useful for testing--you can launch one endpoint with port 0, have it ask the system what port it actually got (getsockaddr), then launch the second endpoint with that port number.

这篇关于检查socket端口是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆