python探测服务器端口

python代码

#导入模块
import socket

def test_port(host,port):
    #创建连接服务对象
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #连接超时3秒
    sk.settimeout(3)
    try:
        #连接服务器ip或域名,端口
        #ip/域名为字符串类型str,端口为数字类型int
        #connect(),内为元组类型
        sk.connect((host, int(port)))
        print(host,port,"is ok")
    #捕捉异常(即连接失败)
    except Exception:
        print("\033[1;31;m",host, port, "is close\033[0m")
    #关闭连接
    sk.close()
if __name__ == '__main__':
    #host="www.baidu.com"
    port = 80
    host = "112.80.248.76"
    test_port(host,port)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>python-socket</title>
</head>
<body>
<p><a href="https://blog.csdn.net/weixin_33736048/article/details/93475477">Python之socket详解</a></p>
</body>
</html>

Python之socket详解可参考https://blog.csdn.net/weixin_33736048/article/details/93475477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值