当前位置:首页 » 网站资讯 » python如何测试网站通不通
扩展阅读
苹果手机实物图片软件 2025-09-22 01:00:31

python如何测试网站通不通

发布时间: 2022-09-26 20:35:26

Ⅰ python3 判断IP地址是否ping通

os.system(‘comand’) 会执行括号中的命令,如果命令成功执行,这条语句返回0,否则返回1。
要想得到标准输出,可以使用os.popen(cmd)
import os
p=os.popen("ping 192.168.2.129")
x=p.read()
p.close()
if x.count('temeout'):
print("ping不通")
else:
print("ping通了")

Ⅱ python如何批量同时检测网址可用性

多线程,目前的运行时间是由网络返回时间决定的,如果你访问的网址不是来自一个网址,没有并发访问数限制,就可以用多线程来提高同时访问的网址数,运行总时间就不是一个串联关系了。不会的话网络一下学习一下就OK了。

Ⅲ 如何用Python实现实时的网络连接检测

如果你用的socket包里的那些阻塞接口,当然写个线程循环监测时间也没啥,只不过记得在循环内加上个sleep,哪怕是1ms甚至1us的sleep都可以避免CPU被消耗干净。

如果你所说的接收是死循环式里跑socket.recv,它会在recv里阻塞,按你的说法3分钟一个心跳包,时间检测就成了3分钟一次,不太合适。

更好的办法自然是通过epoll/poll之类的方式或者asyncio/twisted/tornado之类的异步回调/协程加时间事件甚至是各种GUI框架的事件循环来启动你的发送和接收。考虑到以后可能有多设备,显然利用这些成型的玩意更合理。

Ⅳ 如何用python检测网站可用性

前言
随着站点的增多,管理复杂性也上来了,俗话说:人多了不好带,我发现站点多了也不好管,因为这些站点里有重要的也有不重要的,重要核心的站点当然就管理的多一些,像一些万年都不出一次问题的,慢慢就被自己都淡忘了,冷不丁那天出个问题,还的手忙脚乱的去紧急处理,所以规范的去管理这些站点是很有必要的,今天我们就做第一步,不管大站小站,先统一把监控做起来,先不说业务情况,最起码那个站点不能访问了,要第一时间报出来,别等着业务方给你反馈,就显得我们不够专业了,那接下来我们看看如果用python实现多网站的可用性监控,脚本如下:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

#!/usr/bin/env python

import pickle, os, sys, logging
from httplib import HTTPConnection, socket
from smtplib import SMTP

def email_alert(message, status):
fromaddr = '[email protected]'
toaddrs = '[email protected]'

server = SMTP('smtp.163.com:25')
server.starttls()
server.login('xxxxx', 'xxxx')
server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message))
server.quit()

def get_site_status(url):
response = get_response(url)
try:
if getattr(response, 'status') == 200:
return 'up'
except AttributeError:
pass
return 'down'

def get_response(url):
try:
conn = HTTPConnection(url)
conn.request('HEAD', '/')
return conn.getresponse()
except socket.error:
return None
except:
logging.error('Bad URL:', url)
exit(1)

def get_headers(url):
response = get_response(url)
try:
return getattr(response, 'getheaders')()
except AttributeError:
return 'Headers unavailable'

def compare_site_status(prev_results):

def is_status_changed(url):
status = get_site_status(url)
friendly_status = '%s is %s' % (url, status)
print friendly_status
if url in prev_results and prev_results[url] != status:
logging.warning(status)
email_alert(str(get_headers(url)), friendly_status)
prev_results[url] = status

return is_status_changed

def is_internet_reachable():
if get_site_status('www..com') == 'down' and get_site_status('www.sohu.com') == 'down':
return False
return True

def load_old_results(file_path):
pickledata = {}
if os.path.isfile(file_path):
picklefile = open(file_path, 'rb')
pickledata = pickle.load(picklefile)
picklefile.close()
return pickledata

def store_results(file_path, data):
output = open(file_path, 'wb')
pickle.mp(data, output)
output.close()

def main(urls):
logging.basicConfig(level=logging.WARNING, filename='checksites.log',
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')

pickle_file = 'data.pkl'
pickledata = load_old_results(pickle_file)
print pickledata

if is_internet_reachable():
status_checker = compare_site_status(pickledata)
map(status_checker, urls)
else:
logging.error('Either the world ended or we are not connected to the net.')

store_results(pickle_file, pickledata)

if __name__ == '__main__':
main(sys.argv[1:])

脚本核心点解释:
1、getattr()是python的内置函数,接收一个对象,可以根据对象属性返回对象的值。
2、compare_site_status()函数是返回的是一个内部定义的函数。
3、map() ,需要2个参数,一个是函数,一个是序列,功能就是将序列中的每个元素应用函数方法。
总结
以上就是这篇文章的全部内容,有需要的朋友们可以参考借鉴。

Ⅳ Python 检测一个网址是否为404不存在(网页不存在)

你可以用Python获取网页的状态码,拿到状态码后面怎么处理还不就由你了,见下面截图。

第一种是用urllib模块:
import urllib
status=urllib.urlopen("http://www..com").code
print status

第二种是用requests模块:
import requests
code=requests.get("http://www..com").status_code
print code

Ⅵ Python批量判断网站是否能访问

#coding:utf-8
#author:www.chenhaifei.com
import requests #打开
import time,random #控制时间
import sys #专门乱码的
reload(sys)
sys.setdefaultencoding(‘utf-8’)
headers={
‘User-Agent’:’Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36′,
}
url=’http://www.chenhaifei.com/’ ##检测的url
cont = requests.get(url,allow_redirects = False).status_code ##allow_redirects = False不检测跳转后的状态码
print cont
time.sleep(0.5)
上面是单个判断url状态码的,你可以把想要检测的url放在一个txt里面,这样就可以循环检测了。

Ⅶ python 怎么测试ip的可用性

os.system("ping 测试IP") 去测试,判断返回值

Ⅷ python测试网页打不开的情况下~帮忙看看这个脚本怎么修改啊这个网站是打不开的~但是不写log

既然你是测试网页打不开的情况,所以你try的代码中在打开网页那里就会报错,然后就会执行except的代码,那么当然你写err到file的操作就没有执行,你去cat log ,当然就是空的
你可以把err = 'can not open url' ....... 到下面的代码移动到except中,你再试试

Ⅸ Python 测试网站是否能访问10

input改raw_input

Ⅹ python 判断ip是否能ping通

python编程下,检查IP是否能ping通,并且分别导入两个文件,代码如下:

#!/usr/bin/python
#-*- coding:gb18030 -*-
'''
Created on 2015-7-7
#判断文件中的ip是否能ping通,并且将通与不通的ip分别写到两个文件中
#文件中的ip一行一个
'''
import time,os
start_Time=int(time.time()) #记录开始时间
def ping_Test():
ips=open('host.txt','r')
ip_True = open('ip_True.txt','w')
ip_False = open('ip_False.txt','w')
count_True,count_False=0,0
for ip in ips.readlines():
ip = ip.replace('\n','') #替换掉换行符
return1=os.system('ping -n 2 -w 1 %s'%ip) #每个ip ping2次,等待时间为1s
if return1:
print 'ping %s is fail'%ip
ip_False.write(ip) #把ping不通的写到ip_False.txt中
count_False += 1
else:
print 'ping %s is ok'%ip
ip_True.write(ip) #把ping通的ip写到ip_True.txt中
count_True += 1
ip_True.close()
ip_False.close()
ips.close()
end_Time = int(time.time()) #记录结束时间
print "time(秒):",end_Time - start_Time,"s" #打印并计算用的时间
print "ping通数:",count_True," ping不通的ip数:",count_False
ping_Test()