import socket,time
#coding:UTF-8
#发送的http包头
header_send='GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n'
i=0
#请求次数
num=1
#请求间隔
tinktime=1
#目的地址
ip_dst='www.sina.com.cn'
#目的端口
port_dst=80
while i<num:
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_dst,port_dst))
s.send(header_send)
buffer = []
while True:
    # 每次最多接收1k字节:
    d = s.recv(1024)
    if d:
        buffer.append(d)
    else:
        break
data = b''.join(buffer)
s.close()
header, html = data.split(b'\r\n\r\n', 1)
result='200 OK' in header
if result:
print 'SUCCESS'
print(header.decode('utf-8'))
#print html
# 把接收的数据写入文件:
#with open('D:\python\pythoncode\lfxx\wangzhan\web\sina.html', 'wb') as f:
     #f.write(html)
else:
print 'web error'
i=i+1
time.sleep(tinktime)

posted on 2017-03-03 10:16  watermelon-lf  阅读(2349)  评论(0编辑  收藏  举报