# -*- coding:utf-8 -*-

import urllib
import urllib2
import cookielib
import re

class RuleLoad(object):

    #初始化方法
    def __init__(self):
        #登录的URL
        self.loginURL = "http://172.17.156.38:8288/rtfilter/login"
        #代理IP地址，防止自己的IP被封禁
        #self.proxyURL = 'http://120.193.146.97:843'
        #登录POST数据时发送的头部信息
        self.loginHeaders =  {
            'Host':'172.17.156.38:8288',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
            'Referer' : 'http://172.17.156.38:8288/rtfilter/login.jsp',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Connection' : 'Keep-Alive'
        }
        #用户名
        self.username = 'rtfilter'
        #密码
        self.password = 'rtfilter'
        self.post = post = {
            'username':self.username,
            'password':self.password,
            'refer':'site/'
        }
        #将POST的数据进行编码转换
        self.postData = urllib.urlencode(self.post)
        #设置代理
        #self.proxy = urllib2.ProxyHandler({'http':self.proxyURL})
        #设置cookie
        self.cookie = cookielib.LWPCookieJar()
        #设置cookie处理器
        self.cookieHandler = urllib2.HTTPCookieProcessor(self.cookie)
        #设置登录时用到的opener，它的open方法相当于urllib2.urlopen
        self.opener = urllib2.build_opener(self.cookieHandler,urllib2.HTTPHandler)

    def login(self):
        request = urllib2.Request(self.loginURL,self.postData,self.loginHeaders)
        #得到第一次登录尝试的相应
        response = self.opener.open(request)
        #获取其中的内容
        content = response.read().decode('gbk')
        #获取状态吗
        status = response.getcode()

        return status


    def load_rule(self):
        request = urllib2.Request('http://172.17.156.38:8288/rtfilter/manage?method=2')
        response = self.opener.open(request)
        status = response.getcode()
        return status

if __name__ == '__main__':
    xx = RuleLoad()
    xx.login()
    xx.load_rule()