简体   繁体   English

socket.timeout; 说明?

[英]socket.timeout; Explanation?

I am building a port scanning program ((irrelevant to the question, just explaining the background)), and I know the IP of the host, but not what ports are open. 我正在构建端口扫描程序((与问题无关,仅说明背景)),我知道主机的IP,但不知道打开了哪些端口。 Hence, the scan. 因此,扫描。

It is in the early stages of development, so the error handling is bad, but not bad enough to make why Python does this explainable. 它处于开发的早期阶段,因此错误处理很糟糕,但还不足以使Python为什么可以解释这一点。

It tries to connect to, say, 123.456.7.8, 1 . 它尝试连接到123.456.7.8, 1 Obviously it's a ridiculous port to be open, so it throws an error. 显然,这是一个荒谬的端口,因此它会引发错误。 The error is No Route to Host or the such, right? 错误是“ No Route to Host之类的,对吗? Wrong! 错误! It is instead Operation Timed Out! 而是Operation Timed Out! .

Okay, let's increase the timeout in case my calculations were incorrect. 好的,如果我的计算不正确,请增加超时时间。

. .. ... ....All that did was rinse and repeat! .. .....所做的只是冲洗并重复!

About 20 minutes later, the timeout is at 20 seconds, and it still is timing out. 大约20分钟后,超时时间为20秒,但仍在超时。 Really? 真? Why does python raise a timed out error though, instead of No route to host! 为什么python为什么会引发timed out错误,而不是No route to host! or similar? 或类似的?

I need to distinguish between time outs and connection failures, because there is a difference between late and nowhere. 需要超时和连接失败之间的区别, 因为晚,无处之间的差异。 This prevents me from doing so, creating an infinite loop of hurry up and wait. 这阻止了我这样做,造成了无限循环的急促和等待。

Whatever shall I do? 我该怎么办? Wherever shall I go? 我要去哪里?

Python socket module is a thin wrapper around your platform's socket API. Python socket模块是平台套接字API的薄包装。 The issue is unrelated to Python. 该问题与Python无关。

It is not necessary that you get No Route to Host error. 不一定会出现“ No Route to Host错误。 Moreover it is common that a firewall just drops received packets (for a filtered port) that may manifest as a timeout error in your code. 此外,防火墙通常会丢弃接收到的数据包(用于过滤的端口),这很可能表现为代码中的超时错误。 See Drop vs. Reject (ignore the conclusion but read the explanation of what is happening). 参见丢弃与拒绝 (忽略结论,但阅读正在发生的情况的解释)。

To workaround, make multiple concurrent connections and set a fixed timeout or use raw-sockets and send the packets yourself (you could use scapy , to investigate the behavior). 要解决此问题,请建立多个并发连接并设置固定的超时时间,或者使用原始套接字并自己发送数据包(您可以使用scapy来调查行为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM