|
发表于 2004-4-9 02:09:46
|
显示全部楼层
这是我的,大家参考一下吧
在这里首先要感谢zest,没有他的帮助,我无法这么轻松的搞定, 这里的代码是以他的代码为原型的.感谢他的大公无私.
说明一下, 我的上网方式需要SSL方式
也就是必须通过SSL发送登陆信息, 所以就必须使用OpenSSL了
需要Python的HTTPlib支持SSL
我在SuSE 8.2下, 虽然缺省安装了Python, 但是并不支持SSL,
执行HTTPS访问是, 说什么SSL模块没有安装
如果是这样,那么就需要重新安装Python了
我先down了一个openssl-0.9.7d.tar.gz
安装这个openssl
然后down了Python-2.3.3.tgz
先./configure,
make
make install
这时候就已经可以使用python了,
可以敲个python试试, 如果没有就试试python2.3
然后到Python-2.3.3.tgz的解压目录中
python2.3 setup.py build_ext -I/usr/local/ssl/include -L/usr/local/ssl/lib
(当然前提是你的openssl安装到这两个目录下了,这是缺省目录)
python2.3 setup.py build
python2.3 setup.py install
这样就重新安装了包,并且把ssl也安装进去了.
这时再使用https就不会包错了
##
# bash的脚本
#
# 这一段是照着zest抄的
# 目的是为了获取当前网卡的IP地址
# 因为用的是动态获取IP的方式,所以每次IP都不同
# 我不回用Python获取当前网卡的IP,所以就只好这样凑合一下了
##
# 获取 ip 地址
MYIP=`ifconfig eth0|grep inet|sed 's/^ *//'|sed 's/ /:/g'|cut -f3 -d:`
#echo $MYIP
python2.3 testssl.py --ip=$MYIP > tmpcurl
下面看看我们的主角testssl.py, 原谅我起了一个这样的名字,
import string, getopt, sys, httplib, urllib
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:", ["help", "ip="])
except getopt.GetoptError:
# print help information and exit:
usage()
sys.exit(2)
ip = None
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
if o in ("-i", "--ip"):
ip = a
print ip
#下面的这个IP就是你的认证服务器的IP,这个是我的,你要换成你自己的哦!
HOSTNAME = '61.236.216.67'
conn = httplib.HTTPSConnection(HOSTNAME)
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
'Content-Type': 'application/x-www-form-urlencoded'}
#第一步, 发送请求,这个目的就是为了获取一个sessionID
#http://61.236.216.67
conn = httplib.HTTPConnection(HOSTNAME)
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
'Content-Type': 'application/x-www-form-urlencoded'
}
print headers
conn.request('GET', '/', None, headers)
response = conn.getresponse()
s = response.read()
print "========first response========"
print s
cookie = response.getheader("Set-Cookie")
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': cookie
}
#就是这个cookie
print "=====Cookie====", cookie
#这一步我这里不能少, 需要发送自己的IP, 这里的IP在后面会和刚才session联系起来, 以后keep-alive的参数要用到
#second http://61.236.216.67/webLogin.jsp
conn = httplib.HTTPConnection(HOSTNAME)
conn.request('POST', '/webLogin.jsp', "LocalIP=" + ip, headers)
response = conn.getresponse()
s = response.read()
print "========first response========"
print s
print "=====headers====", headers
#第三步, 这是最关键的一步, 就是发送认证信息的请求
#third https://61.236.216.67:443/secu/webLogin.jsp
params = urllib.urlencode({'connectname': '',
'connecttype': -1,
'consumeright': 0,
'separatecard': 0,
'localip': ip,
'IsIndex': 0,
'username': [这里写你的上网帐号,就是用户名,最好用单引号括起来,没有外面的中括号],
'password': [这里写你的口令,最好用单引号括起来,没有外面的中括号],
'cookiedate': 0})
print params
conn.request('POST', '/secu/webLogin.jsp', params, headers)
response = conn.getresponse()
s = response.read()
start = string.find(s, HOSTNAME)
end = string.find(s, "')", start)
s = s[start:end]
URL = s
cookie2 = response.getheader("Set-Cookie")
print URL
print "cookie2----->>>",cookie2
#第四步, 获取注册信息,这是keep-alive的关键步骤,这里的请求地址是上个response返回的.
#four get regist parameters
conn = httplib.HTTPConnection(HOSTNAME)
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': cookie
}
start = string.find(URL, "/")
end = string.find(URL, "?", start)
URL2 = URL[start:end]
params = URL[end+1:]
print "========deal response========"
print URL2, headers
print "=====params====", params
conn.request('GET', URL2, params, headers)
response = conn.getresponse()
s = response.read()
print s
start = string.find(s, "ortalClient.loginsuc(")
end = string.find(s, ");", start)
print s[start:end]
print "kl-session=",cookie
print "get-session=",response.getheader("Set-Cookie")
#loginstr = s[start:end]
loginstr = s[start + 23:end]
loginstr = string.replace(loginstr, "\r", "")
loginstr = string.replace(loginstr, "\n", "")
loginstr = string.replace(loginstr, " ", "")
loginstr = string.replace(loginstr, "'", "")
print loginstr
L = string.split(loginstr, ',')
print L
user = L[0]
isnno = '1204'
loginkey = L[6]
ip = L[7]
#第五步, 这时已经获取了注册信息,但是还不能keep-alive, 这一步是请求一个新的sessionID, 这个ID才是keep-alive用的
path = '/ClientProcess.jsp?MsgType=2&UserName=' + user + '&LocalIP=' + ip + '&ConnectType=1&ConsumeRight=0&ISNNO=' + isnno + '&LoginKey=' + loginkey
print path
conn = httplib.HTTPConnection(HOSTNAME)
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)'}
conn.request('GET', path, None, headers)
response = conn.getresponse()
kl_cookie = response.getheader("Set-Cookie")
print "kl_cookie=",kl_cookie
#好了,终于获取了所有要的参数, 收工了.
看看kl.py吧
import string, getopt, sys, httplib, urllib
def main():
#这里要读取刚才输出的tmpcurl文件, 所以一定不要认为那个文件没有用就删除掉,
f = open("/这里是绝对路径/tmpcurl")
print keepalive(f.read())
def keepalive(loginstring):
#这个是认证服务器的IP
server = '61.236.216.67'
mark = "ortalClient.loginsuc('"
start = string.find(loginstring, mark)
end = string.find(loginstring, ");", start)
loginstr = loginstring[start + 23:end]
loginstr = string.replace(loginstr, "\r", "")
loginstr = string.replace(loginstr, "\n", "")
loginstr = string.replace(loginstr, " ", "")
loginstr = string.replace(loginstr, "'", "")
print loginstr
L = string.split(loginstr, ',')
#获取到我们需要的注册信息
print L
user = L[0]
msgtype = L[4]
isnno = '1204'
loginkey = L[6]
ip = L[7]
#获取到我们keep-alive的sessionID,就是最后那个请求的session
#get session
start = string.find(loginstring, 'kl_cookie=')
end = string.find(loginstring, ';', start)
cookie = loginstring[start+11:end+1]
path = '/ClientProcess.jsp?MsgType=1&LocalIP=' + ip + '&ISNNO=' + isnno + '&LoginKey=' + loginkey
print path, cookie
req = httplib.HTTP(server)
req.putrequest('GET', path)
req.putheader('Accept', 'text/html')
req.putheader('User-Agent', 'Mozilla')
req.putheader('Cookie', cookie)
req.endheaders()
ec, em, h = req.getreply()
fd = req.getfile()
return fd.read(), (ec, em)
if __name__ == "__main__":
main()
好了,现在,你只要每10分钟记得运行一下kl.py就好了,,但是有点累, 还是让linux做吧
用corn来做吧,
写个文件,里面就一句话:
*/5 * * * * python /绝对路径/kl.py
然后用
corntab 文件名
注册这个文件,
好了,万事大吉了.好好上网吧 |
|