#导入Python标准库中的httplib
import httplib
import sys
#连接百度主页
conn=httplib.HTTPConnection('www.baidu.com',timeout=60)
#尝试获取验证页面
conn.request('GET','/')
resp1=conn.getresponse()
print(resp1.status)
print(resp1.reason)
checkPage=resp1.read()
print(checkPage)
#检索magiccode
magicpos = checkPage.find('magic')
#如果没有检索到magic,说明正常连接到百度,网络已经验证
if(magicpos==-1):
print('Already authenticate')
sys.exit(0)
first = checkPage.find('\"',magicpos+7)
last = checkPage.find('\"',first+1)
#截取magic
magicCode = checkPage[first+1:last]
print(magicCode)
#使用post方法发送验证数据到网关
postData = {'username':'******','password':'*******','magic':magicCode,'4Tredir':'/'}
param = urllib.urlencode(postData)
conn.request('POST','/',param)
resp2 = conn.getresponse()
print(resp2.status)
print(resp2.reason)
print(resp2.read())
print('Authentication was successful.')
上一篇
(转)西藏驼路
2009-04-11