![在 Windows 中是否有办法检查可以打开哪个端口](https://linux22.com/image/794502/%E5%9C%A8%20Windows%20%E4%B8%AD%E6%98%AF%E5%90%A6%E6%9C%89%E5%8A%9E%E6%B3%95%E6%A3%80%E6%9F%A5%E5%8F%AF%E4%BB%A5%E6%89%93%E5%BC%80%E5%93%AA%E4%B8%AA%E7%AB%AF%E5%8F%A3.png)
我遇到一个问题,无法打开端口 64120 到 64130。Eclipse SonarLint 插件需要这些端口。
端口未被使用,已使用 netstat、tcpview 和 portqry 检查。有某些东西或某些设置阻止打开这些端口。
我想创建一个所有不能打开的端口的列表。我希望如果我知道被阻止的范围,我就能找出阻止它们的原因。
是否有命令可以创建这样的列表?
Windows 11 企业版 23H2
端口阻塞问题如何体现
PS C:\workspace> netstat -anp | findstr 64120
没有返回任何内容,而且 tcpview 和其他实用程序也没有显示端口 64120 正在使用中。
- Sonarlint 插件在启动时显示错误
Starting SonarLint for Eclipse 9.3.3006
Error while starting port: 64120, Address already in use: bind
Error while starting port: 64121, Address already in use: bind
Error while starting port: 64122, Address already in use: bind
Error while starting port: 64123, Address already in use: bind
Error while starting port: 64124, Address already in use: bind
Error while starting port: 64125, Address already in use: bind
Error while starting port: 64126, Address already in use: bind
Error while starting port: 64127, Address already in use: bind
Error while starting port: 64128, Address already in use: bind
Error while starting port: 64129, Address already in use: bind
Error while starting port: 64130, Address already in use: bind
- 使用 python tcp_server 示例
https://docs.python.org/3/library/socketserver.html
python3.11.exe c:/cce/workspaces/develop128/tcp_server.py
Traceback (most recent call last):
File "c:\workspace\tcp_server.py", line 24, in <module>
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\socketserver.py", line 456, in __init__
self.server_bind()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\socketserver.py", line 472, in server_bind
self.socket.bind(self.server_address)
PermissionError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
防火墙 ?
我怀疑防火墙和其他系统管理工具可能是原因,但目前我无法检查。我想创建阻止端口列表,以便我可以将其传达给我们的系统管理员
答案1
我创建了一个小型 Python 程序,尝试打开所有端口并列出结果。
import socket
def check_port(port):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('0.0.0.0', port))
result = "available"
except OSError as e:
result = f"closed: {e}"
return result
def main():
# Define the range of ports you want to check
start_port = 2
end_port = 65535
first_port = 1
last_port = first_port
first_result = check_port(first_port)
for port in range(start_port, end_port + 1):
result = check_port(port)
if (result == first_result):
last_port = port
if (result != first_result or port == end_port):
print(f"{first_port}-{last_port} : {first_result}")
first_port = port
last_port = first_port
first_result = result
if __name__ == "__main__":
main()
样本输出(已修剪)
python -u "tcp_testports.py"
...
2180-3388 : available
3389-3389 : closed: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
3390-5039 : available
5040-5040 : closed: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
5041-5984 : available
5985-5985 : closed: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
5986-7679 : available
...
端口锁定问题已消失。我禁用了 Internet 连接共享服务并重新启动。这似乎解决了问题,但当我检查时发现 ICS 服务已重新启动,因此我不确定是什么阻塞了端口。我会保留此代码以防再次遇到同样的问题。