当前位置: 首页 > news >正文

【Python】Python 2 测试网络连通性脚本

文章目录

    • 前言
    • 1. 命令行传参
    • 2. 代码

前言

最近在只有python2的服务器上部署服务,不能用三方类库,这里出于好奇心学习下python。这里简单做个脚本,实现了检验网络连通性的功能(类似于curl)。
在这里插入图片描述

1. 命令行传参

使用命令行的目的是可以一次性检验多个url的连通性,比如上线前对接多个三方接口可以用上。

python test_socket.py -u "{'https://www.baidu.com':'120.232.145.144','ws://localhost:8080/ws':'127.0.0.1','wss://hello/ws':''}" 

参数解释

{'https://www.baidu.com':'120.232.145.144', // 期待域名解析为120.232.145.144 否则报错'ws://localhost:8080/ws':'127.0.0.1',  // 期待域名解析为127.0.0.1 否则报错'wss://hello/ws':''  // 无期望域名解析的ip,保证连通性即可
}

2. 代码

# -*- coding: utf-8 -*-# coding:utf-8
import sys
import getopt
import socket
import urlparse
import redef get_hostname(input_url):if len(input_url) == 0:return 'url为空'hostname = urlparse.urlparse(input_url).hostnameif hostname is None:return '域名解析为空'try:return socket.gethostbyname(hostname)except socket.error:return '域名解析异常'def get_port(input_url):if len(input_url) == 0:return 'url为空'# url 上读端口号input_port = urlparse.urlparse(input_url).portif input_port:return input_port# url 上读不到端口号用协议类型区分scheme = urlparse.urlparse(input_url).schemeif scheme == 'https' or scheme == 'wss':return 443if scheme == 'http' or 'ws':return 80def check_connect(input_ip, input_port):# 设置超时时间为3秒钟socket.setdefaulttimeout(3)s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)try:s.connect((input_ip, input_port))return '可联通'except socket.timeout:return '无法联通, 超时'except Exception as e:return '无法联通, 异常:' + str(e)finally:s.close()# 从命令行中读取参数
opts, argv = getopt.getopt(sys.argv[1:], 'u:')
ops = dict(opts)
url_ip_dict = eval(ops.get('-u'))# 循环参数内容进行连通性检测
print "-"*150
printTemplate = "%-60s %-20s %-20s %-50s"
print printTemplate % ('url', 'ip', 'port', 'result')
print "-"*150
for input_url, input_ip in url_ip_dict.iteritems():# 域名解析actual_ip = get_hostname(input_url)# 未能解析出ip为异常if not re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', input_ip):print printTemplate % (input_url, '\\', '\\', actual_ip)continue# 解析出ip, 且入参存在ip 则校验是否是解析出来的ipif input_ip:if actual_ip != input_ip:print printTemplate % (input_url, input_ip, '\\', '未能联通, 实际解析的ip为' + actual_ip)continueport = get_port(input_url)# 连通性测试result = check_connect(actual_ip, port)print printTemplate % (input_url, actual_ip, port, result)
http://www.lryc.cn/news/376987.html

相关文章:

  • 瓦罗兰特教你怎么玩低价区+超适配低价区的免费加速器
  • Linux安装kvm虚拟机
  • @pytest.fixture与@pytest.mark.parametrize结合实现参数化
  • uniapp中unicloud接入支付宝订阅消息完整教程
  • PHP 标准建议psr
  • 第九届信也科技杯全球AI算法大赛——语音深度鉴伪识别参赛A榜 0.968961分
  • 【设计模式(三) 设计模式的分类 】
  • Linux进程概念(个人笔记)
  • 每天五分钟计算机视觉:如何在现有经典的卷积神经网络上进行微调
  • 10个典型的MySQL笔试题和面试题
  • AI大模型的TTS评测
  • 推荐一款可以下载B站视频和音频的工具
  • 中科数安 |-透明加密软件_无感透明加密 - 源头有保障
  • ui自动化selenium,清新脱俗代码,框架升级讲解
  • 【吊打面试官系列-Mysql面试题】Myql 中的事务回滚机制概述 ?
  • VMware虚拟机三种网络模式设置 - Bridged(桥接模式)
  • 关于Panabit在资产平台中类型划分问题
  • 【C语言】12.C语言内存函数
  • Django:如何将多个数据表内容合在一起返回响应
  • 棱镜七彩荣获CNNVD两项大奖,专业能力与贡献再获认可!
  • uni-app中使用富文本rich-text个人经验
  • Matlab|基于V图的配电网电动汽车充电站选址定容-可视化
  • 从零开始! Jupyter Notebook的安装教程
  • web前端信息卡:深入探索与实用指南
  • 之所以选择天津工业大学,因为它是双一流、报考难度适宜,性价比高!天津工业大学计算机考研考情分析!
  • WPF三方UI库全局应用MessageBox样式(.NET6版本)
  • ABAP-03基础数据类型
  • Zabbix监控神通数据库教程
  • 5.音视频基础 FLV
  • Ubuntu server 24 (Linux) 安装客户端(windows/linux) Zabbix 7.0 LTS Zabbix agent2