使用Python检查CDN节点


使用Python检查CDN节点是否正常


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python 2.7

import socket, time

start = time.time()

ip = '61.240.149.153'

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

# 建立连接:
s.connect((ip, 80))

# 发送数据:
s.send('GET /test/ HTTP/1.1\r\nHost: www.test.com\r\nConnection: close\r\n\r\n')

# 接收数据:
buffer = []
while True:
    # 每次最多接收1k字节:
    d = s.recv(1024)
    if d:
        buffer.append(d)
    else:
        break

data = ''.join(buffer)

# 关闭连接:
s.close()

status, other = data.split('\r\n', 1)

end = time.time() - start

print 'IP : %s Status : %s Time : %s' % (ip, status, end)