简体   繁体   English

检查服务是否可达并使用 Python 中的套接字模块

[英]Check if service is reachable and working with socket module in Python

The context related to my question is that I work for a company in the networking area.与我的问题相关的背景是我在网络领域的一家公司工作。 This company has several stores around the country where DVRs are accessible through port 2781 and a domain for people to access security cameras, the problem is that in order for these people to successfully access DVRs through the domain and port you must have a DMZ configured in the modem of the stores.这家公司在全国有几家商店,可以通过端口 2781 访问 DVR 和一个供人们访问安全摄像头的域,问题是为了让这些人通过域和端口成功访问 DVR,您必须在 DMZ 中配置商店的调制解调器。 To corroborate the DMZ I'm trying to use Python with the sockets module but I don't understand the best way to do it yet.为了证实 DMZ,我正在尝试将 Python 与 sockets 模块一起使用,但我还不明白最好的方法。

import socket
    
s = socket.socket()
s.connect((domain, port))
s.close()

Once I make the proper connection which is the best way to check if there is a communication?一旦我建立了正确的连接,这是检查是否有通信的最佳方法? Work it with an exception or just use socket.recv and detect if it is empty?处理异常还是只使用 socket.recv 并检测它是否为空?

In order for connect to succeed there already has to be some kind of connection be done.为了使connect成功,必须完成某种连接。 Otherwise the TCP handshake would fail.否则 TCP 握手将失败。 Thus the first step would be to check if connect succeeds or throws an exception.因此,第一步是检查connect是否成功或抛出异常。

It can still be possible that there is some deep packet inspection firewall in place which does not block the initial connection but only blocks the later data exchange.仍然有可能存在一些深度数据包检查防火墙,它不会阻止初始连接,但只会阻止以后的数据交换。 To find out if this is the case you have to do actual bidirectional communication.要确定是否是这种情况,您必须进行实际的双向通信。 But how this communication should look like depends on the specific application protocol which is unknown in your case.但是这种通信应该是什么样子取决于在您的情况下未知的特定应用程序协议。 Still you need to check that a) sending and receiving works (catching exceptions) and b) that a response returns the expected data.您仍然需要检查 a) 发送和接收是否正常工作(捕获异常)和 b) 响应是否返回预期数据。

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

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