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)
#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)
本文介绍了一个使用Python Socket库实现的简易HTTP GET请求发送及响应解析程序。该程序能够向指定服务器(如新浪网)发起HTTP GET请求,并解析接收到的HTTP响应头部。通过循环调用的方式,可以按设定的次数和间隔重复发送请求。
&spm=1001.2101.3001.5002&articleId=60126903&d=1&t=3&u=5c48234a8deb42d6bbe2d188ae2bdfdd)
4639

被折叠的 条评论
为什么被折叠?



