随笔——python截取http请求报文响应头

Request.add_header——作用是在http报文中加入条目

page.info().getheader("item")——用来获取http响应头中的item条目内容

import urllib2
import urllib
from types import *
import hashlib

req = urllib2.Request('http://script2.iscc.org.cn/web07_e3a95260b7271954aa59460c134cde7e/')
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0)')    
req.add_header('Host', 'script2.iscc.org.cn')       
req.add_header('Connection', 'keep-alive')  
req.add_header('Referer', 'http://www.iscc.org.cn/challenges/2014/web/web06/')  
 
urllib2.socket.setdefaulttimeout(10)  
page = urllib2.urlopen(req)
password=page.info().getheader("PassWord")
cookie=page.info().getheader("Set-Cookie")
cookie=cookie[0:45]
print password
print cookie
data = page.read()
hash_value = hashlib.md5(password).hexdigest().upper()
print hash_value
print len(data)  

test_data = 'pwd='+hash_value+'&Submit=%E6%8F%90%E4%BA%A4'
print test_data

requrl = "http://script2.iscc.org.cn/web07_e3a95260b7271954aa59460c134cde7e/index.asp?action=Check"
req = urllib2.Request(url = requrl,data =test_data)
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0)')    
req.add_header('Cookie', cookie)  
req.add_header('Referer', ' http://script2.iscc.org.cn/web07_e3a95260b7271954aa59460c134cde7e/')  
res_data = urllib2.urlopen(req)
res = res_data.read()
f = open("C:\\1.html", 'wb')
f.write(res)
f.close()
print res


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值