python检测远程服务器tcp端口的方法
作者:chongq 时间:2021-02-18 22:49:44
本文实例讲述了python检测远程服务器tcp端口的方法。分享给大家供大家参考。具体如下:
python检测远程服务器tcp端口的代码,这段代码可以用来做服务器监控实用
#!/usr/bin/env python
#coding:utf-8
#filename:tcp.py
'''
author: gavingeng
date: 2011-12-14 09:35:59
'''
import socket
import sys
NORMAL=0
ERROR=1
TIMEOUT=5
def ping(ip,port,timeout=TIMEOUT):
try:
cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
address=(str(ip),int(port))
status = cs.connect_ex((address))
cs.settimeout(timeout)
#this status is returnback from tcpserver
if status != NORMAL :
print ERROR
else:
print NORMAL
except Exception ,e:
print ERROR
print "error:%s" %e
return ERROR
return NORMAL
if __name__=='__main__':
if len(sys.argv) < 3 :
print ur'请按照如下格式使用: ./tcp.py www.jb51.net 80'
sys.exit(1)
ip = sys.argv[1]
port = sys.argv[2]
try:
timeout = sys.argv[3]
except IndexError ,e:
timeout=TIMEOUT
ping(ip,port,timeout)
希望本文所述对大家的Python程序设计有所帮助。
标签:python,服务器,tcp,端口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 开发工具PyCharm安装教程图文详解(新手必看)
2023-04-25 05:01:08
![](https://img.aspxhome.com/file/2023/0/100810_0s.jpg)
Python基于分水岭算法解决走迷宫游戏示例
2021-08-04 17:41:37
PHP中DirectIO操作文件扩展的用法大全
2023-06-13 13:58:45
Django利用AJAX技术实现博文实时搜索
2023-01-02 01:57:50
![](https://img.aspxhome.com/file/2023/3/80583_0s.jpg)
python从入门到实践之组合数据类型
2021-09-02 17:53:54
天极产品设计流程
2007-10-11 18:47:00
![](https://img.aspxhome.com/file/UploadPic/200710/11/20071011184910229s.jpg)
python搜索包的路径的实现方法
2021-01-18 01:27:44
Python中关于Sequence切片的下标问题详解
2021-05-30 22:49:47
JavaScript设计模式之适配器模式介绍
2023-08-12 06:36:09
Python3 列表,数组,矩阵的相互转换的方法示例
2023-05-30 23:52:19
![](https://img.aspxhome.com/file/2023/1/114521_0s.png)
python,pycharm的环境变量设置方式
2023-08-04 10:44:41
![](https://img.aspxhome.com/file/2023/0/101060_0s.png)
PHP实现断点续传乱序合并文件的方法
2023-06-20 02:35:21
Python获取航线信息并且制作成图的讲解
2023-08-28 18:18:56
![](https://img.aspxhome.com/file/2023/6/61986_0s.png)
Python cookbook(数据结构与算法)从任意长度的可迭代对象中分解元素操作示例
2023-12-28 10:13:11
![](https://img.aspxhome.com/file/2023/2/107532_0s.png)
Python学习笔记之解析json的方法分析
2022-01-08 05:01:28
![](https://img.aspxhome.com/file/2023/5/133615_0s.png)
Python递归遍历列表及输出的实现方法
2022-12-08 17:08:11
Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
2021-10-19 05:05:53
微信小程序学习笔记之表单提交与PHP后台数据交互处理图文详解
2023-11-22 19:29:44
![](https://img.aspxhome.com/file/2023/7/111307_0s.png)
php插件功能实现方法介绍
2023-05-30 12:28:52
教你快速掌握数据库查询优化的实用技巧
2008-11-28 15:10:00