python测试网络连通性_PYTHON 测试服务器连通性

本文使用Python进行网络连通性测试。通过输入要测试的site值,对电信和联通IP进行端口测试、域名解析测试、ping测试,还进行了页面打开测试。利用urllib2、socket、subprocess等库实现各项功能,输出测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#-*-coding:utf-8-*-

import os

import sys

import urllib2

import pygame

import re

import socket

import subprocess

# 输入要测试的site值

print "Hello"

print pygame.ver

site=raw_input("Pls Input Your Site:")

req = urllib2.Request('http://192.168.1.88/cc.txt')

fd = urllib2.urlopen(req)

#http://192.168.1.88/cc.txt连接文件的第一列为site,第二列为电信IP,第三列为联通IP,第四列为域名

for i in fd.readlines():

i2 = re.match(site,i)

if i2:

SITE,DXIP,LTIP,DOMAIN=i.split()

AGENT,ID=SITE.split('_')

strinfo = re.compile('^0*')

id = strinfo.sub('',ID)

domain = 's'+id+"."+DOMAIN

#端口测试

for PORT in (1001,1002,1003):

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.settimeout(1)

DXADDR = (str(DXIP),int(PORT))

dxstatus = s.connect_ex(DXADDR)

LTADDR = (str(DXIP),int(PORT))

ltstatus = s.connect_ex(LTADDR)

if dxstatus == 0:

print 'Test DX Connection to %s %s port [tcp] OK' % (DXIP,PORT)

else:

print 'Test DX Connection to %s %s port [tcp] FAIL' % (DXIP,PORT)

if ltstatus == 0:

print 'Test LT Connection to %s %s port [tcp] OK' % (LTIP,PORT)

else:

print 'Test lT Connection to %s %s port [tcp] FAIL' % (LTIP,PORT)

continue

#域名解析测试

domainping = subprocess.call("ping -n 1 %s" % domain, shell=True, stdout=open(r'ping.temp','w'), stderr=subprocess.STDOUT)

if domainping == 0:

print "Domain name resolution %s: is OK"  %   domain

else:

print "Domain name resolution %s: is FAIL" %  domain

#ping 测试 电信IP不行,就测试联通IP

dxping = subprocess.call("ping -n 1 %s" % DXIP, shell=True, stdout=open(r'ping.temp','w'), stderr=subprocess.STDOUT)

ltping = subprocess.call("ping -n 1 %s" % LTIP, shell=True, stdout=open(r'ping.temp','w'), stderr=subprocess.STDOUT)

if dxping == 0:

print "ping DX %s: is OK" %  DXIP

else:

print "ping DX %s is  FAIL"   % DXIP

if ltping == 0:

print "ping LT %s: is OK" % LTIP

else:

print "ping LT %s is  OK"   % LTIP

#页面打开测试

req1 = urllib2.Request('http://%s/aa.jsp' % domain)

try:

urllib2.urlopen(req1)

print 'WebServer OK'

except urllib2.URLError, e:

print e.reason

#i3=fd1.readlines()

#m1=re.search(site,str(i3))

raw_input("Pls Input Any Key.......")

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值