示例#1
0
文件: Query.py 项目: zvrr/12306-1
    def query(flag,
              base_url,
              trainDate,
              fromStation,
              toStation,
              passengerType=PASSENGER_TYPE_ADULT):
        # params = {
        #     r'leftTicketDTO.train_date': trainDate,
        #     r'leftTicketDTO.from_station': city2code(fromStation),
        #     r'leftTicketDTO.to_station': city2code(toStation),
        #     r'purpose_codes': passengerType
        # }
        params = collections.OrderedDict()
        params['leftTicketDTO.train_date'] = trainDate
        params['leftTicketDTO.from_station'] = city2code(fromStation)
        params['leftTicketDTO.to_station'] = city2code(toStation)
        params['purpose_codes'] = passengerType

        if flag > 1:
            jsonRet = EasyHttp.send(queryUrls['query'], params=params)
        else:
            for suffix in ['', 'O', 'X', 'Z', 'A', 'T', 'V']:
                queryUrls['query']['url'] = base_url + suffix
                jsonRet = EasyHttp.send(queryUrls['query'], params=params)
                if jsonRet:
                    break
        try:
            if jsonRet:
                return Query.__decode(jsonRet['data']['result'], passengerType)
        except Exception as e:
            Log.e(e)

        return []
示例#2
0
文件: Query.py 项目: xzgxp/EasyTrain
 def query(session,
           trainDate,
           fromStation,
           toStation,
           passengerType=PASSENGER_TYPE_ADULT):
     params = {
         r'leftTicketDTO.train_date': trainDate,
         r'leftTicketDTO.from_station': city2code(fromStation),
         r'leftTicketDTO.to_station': city2code(toStation),
         r'purpose_codes': passengerType
     }
     # response = NetUtil.normalGet(Query.__url, params=params, headers={'User-Agent': random.choice(USER_AGENT)}).json()
     headers = {
         'User-Agent': FIREFOX_USER_AGENT,
         'Referer': r'https://kyfw.12306.cn/otn/leftTicket/init',
         'Connection': 'keep-alive',
     }
     response = NetUtils.get(session,
                             Query.__url,
                             params=params,
                             headers=headers,
                             verify=False)
     print(response.text)
     response = response.json()
     response = response['data'] if 'data' in response else Log.e(
         "can not find key: 'data'")
     queryResults = response['result'] if 'result' in response else Log.e(
         "can not find key: 'result'")
     return Query.__decode(queryResults, passengerType)
示例#3
0
文件: Login.py 项目: Faiz56/12306
    def _loginNormal(self, userName, userPwd, autoCheck=2):
        self._init()
        self._uamtk()
        if autoCheck == CAPTCHA_CHECK_METHOD_THREE:
            results, verify = Captcha().verifyCodeAuto()
        elif autoCheck == CAPTCHA_CHECK_METHOD_HAND:
            results, verify = Captcha().verifyCaptchaByHand()
        else:
            results, verify = Captcha().verifyCodeAutoByMyself()

        if not verify:
            return False, '验证码识别错误!'
        Log.v('验证码识别成功')
        payload = {
            'username': userName,
            'password': userPwd,
            'appid': 'otn',
            'answer': results
        }
        jsonRet = EasyHttp.send(self._urlInfo['login'], data=payload)

        def isLoginSuccess(responseJson):
            return 0 == responseJson['result_code'] if responseJson and 'result_code' in responseJson else False, \
                   responseJson[
                       'result_message'] if responseJson and 'result_message' in responseJson else '登录失败'

        result, msg = isLoginSuccess(jsonRet)
        if not result:
            return False, msg
        # self._userLogin()
        self._passportRedirect()
        result, msg, apptk = self._uamtk()
        if not Utils.check(result, msg):
            return False, 'uamtk failed'
        return self._uamauthclient(apptk)
    def __init__(self):

        if app.config['SQL_DRIVER'] == 'pymssql':
          engine = create_engine(r"mssql+pymssql://{0}:{1}@{2}:{3}/{4}".format(
                                          app.config['DATABASE_USER'],
                                          app.config['DATABASE_PASSWORD'],
                                          app.config['DATABASE_HOST'],
                                          app.config['DATABASE_PORT'],
                                          app.config['DATABASE_NAME']))

        else:
          quoted = urllib.quote_plus('DRIVER={FreeTDS};Server=%s;Database=%s;UID=%s;PWD=%s;TDS_Version=8.0;CHARSET=UTF8;Port=1433;'
                                          %(app.config['DATABASE_HOST'],
                                            app.config['DATABASE_NAME'],
                                            app.config['DATABASE_USER'],
                                            app.config['DATABASE_PASSWORD']))

          engine = create_engine('mssql+pyodbc:///?odbc_connect={}'.format(quoted), connect_args={'convert_unicode': True})

        # create a Session
        Session = sessionmaker(bind=engine)

        try:
            self.session = Session()
            event.listen(Session, "after_transaction_create", self.session.execute('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'))
            
            Log.info('Connection Openned')
        except:
            Log.info('Can\'t create database session')
            raise
示例#5
0
def main():
    login = Login()
    Log.v('正在登录...')
    result, msg = login.login(USER_NAME, USER_PWD)
    if not Utils.check(result, msg):
        Log.e(msg)
        return
    Log.v('%s,登录成功' % msg)

    seatTypesCode = SEAT_TYPE_CODE if SEAT_TYPE_CODE else [SEAT_TYPE[key] for key in SEAT_TYPE.keys()]
    passengerTypeCode = PASSENGER_TYPE_CODE if PASSENGER_TYPE_CODE else '1'
    while True:
        # 死循环一直查票,直到下单成功
        try:
            print('-' * 40)
            ticketDetails = Query.loopQuery(TRAIN_DATE, FROM_STATION, TO_STATION,
                                            TrainUtils.passengerType2Desc(passengerTypeCode),
                                            TRAINS_NO,
                                            seatTypesCode)
            Log.v('已为您查询到可用余票:%s' % ticketDetails)

            ticketDetails.passengersId = PASSENGERS_ID
            ticketDetails.ticketTypeCodes = passengerTypeCode
            ticketDetails.tourFlag = TOUR_FLAG if TOUR_FLAG else 'dc'
            submit = Submit(ticketDetails)
            if submit.submit():
                submit.showSubmitInfoPretty()
                break
            time.sleep(1)
        except Exception as e:
            Log.w(e)
    print(login.loginOut())
示例#6
0
    def _getExtraInfo(self):
        def getRepeatSubmitToken(html):
            repeatSubmitToken = re.findall(
                r"var globalRepeatSubmitToken = '(.*)'", html)[0]
            # print('RepeatSubmitToken = %s' % repeatSubmitToken)
            return repeatSubmitToken

        formData = {'_json_att': ''}
        extra_info = copy.deepcopy(self._urlInfo['getExtraInfo'])
        # extra_info['headers']['Referer'] = self._urlInfo['getExtraInfo']['headers']['Referer']+ '?linktypeid='+self.__ticket.tourFlag
        response = EasyHttp.post_custom(extra_info, data=formData)

        if response and response.status_code == requests.codes.ok:
            html = response.text
            if html.find('系统忙,请稍后重试') != -1:
                Log.e('系统忙,请稍后重试...')
                return False
        else:
            html = None
        # html = EasyHttp.send(self._urlInfo['getExtraInfo'])
        if not Utils.check(
                html,
                'failed to visit %s' % self._urlInfo['getExtraInfo']['url']):
            return False
        self.__ticket.repeatSubmitToken = getRepeatSubmitToken(html)

        def decodeTicketInfoForPassengerForm(html):
            ticketInfoForPassengerForm = re.findall(
                r'var ticketInfoForPassengerForm=(.*);', html)[0]
            return json.loads(ticketInfoForPassengerForm.replace("'", "\""))

        # print('=======================')
        self.__ticket.ticketInfoForPassengerForm = decodeTicketInfoForPassengerForm(
            html)
        return True
示例#7
0
文件: Peer.py 项目: mapa17/Eruliaf
    def getNewPeerList(self):
        
        #Dont create too many connections to other peers ( because of performance problems )        
        if(len(self._peersConn) >= self._maxPeerListSize):
            return
        t = SSimulator().tick
        if( (t - self._lastPeerListUpdate) < self._timeBetweenPeerListUpdates):
            return
        else:
            self._lastPeerListUpdate = t
        
        Log.pLD(self, "Ask Tracker for new peers ...".format())
        newPeers = self._torrent.tracker.getPeerList()

        #Filter unwanted peers , for example itself
        def f(x): return ( x.pid != self.pid )
        newPeers = filter( f, newPeers )

        for i in newPeers :
            if (i.pid in self._peersConn) == False:
                newConnection = Connection(self, i)
                self._peersConn[i.pid] = ( 0, i.pid, newConnection, -1 , self.NO_SLOT )
                newConnection.setUploadLimit( self._calculateUploadRate(newConnection) )
                #newConnection.setDownloadLimit( self._calculateDownloadRate(newConnection) )
                newConnection.connect()
                Log.pLD(self, "adding Peer [{0}]".format(i.pid))
示例#8
0
    def verifyCodeAuto(self, type=TYPE_LOGIN_NORMAL_WAY):
        try:
            response = EasyHttp.send(autoVerifyUrls['12305'])

            if response['result_code'] != '0':
                return None, False
            img_base64 = response['image']

            body = {'base64': img_base64}
            response = EasyHttp.send(autoVerifyUrls['api'], json=body)

            if response['success'] != True:
                return None, False
            body = {
                'check': response['check'],
                'img_buf': img_base64,
                'logon': 1,
                'type': 'D'
            }
            response = requests.post(autoVerifyUrls['img_url']['url'],
                                     json=body).json()
            content = str(response['res'])
            results = content.replace('(', '').replace(')', '')

        except Exception as e:
            Log.w(e)
            return None, False
        return results, self._captchaAutoCheck(results)
示例#9
0
文件: Peer_C1.py 项目: mapa17/Eruliaf
 def _dropPeers(self, maxPeers):
     p = list(self._peersConn)
     nDrop = len(p) - maxPeers
     if(nDrop < 0):
         return #Nothing to do
     
     while(nDrop > 0 and len(p) > 0):
         pID = random.sample(p, 1)[0]
         #print("Dropping peer {} from {}".format(pID, p))
         p.remove(pID)
             
         c = self._peersConn[pID]
         if(c[2].interested == True):
             continue #Do not drop peers we are interested in
         
         if(c[2].peerIsInterested() == True):
             continue #Do not drop peers that have interest in our data
         
         if(c[4] != Peer.NO_SLOT):
             continue #Do not drop active connection of any kind
        
         if(c[2].getDownloadRate() > 0):
             continue #Do not drop peers we are downloading from
         
         Log.pLD(self, "Dropping peer [{}]".format( c[1]) )
         self._peersConn[pID][2].disconnect()
         nDrop-=1
示例#10
0
 def validUsefulProxy(params):
     """
     检验代理是否可用
     """
     marks = params.split('&&')
     if isinstance(marks[1], bytes):
         marks[1] = marks[1].decode('utf8')
     proxies = {"http": "http://{proxy}".format(proxy=marks[1])}
     flag = None
     try:
         # 超过20秒的代理就不要了
         r = requests.get('http://httpbin.org/ip',
                          proxies=proxies,
                          timeout=10,
                          verify=False)
         if r.status_code == 200 and r.json().get("origin"):
             # logger.info('%s is ok' % proxy)
             flag = True
     except Exception as e:
         flag = False
     if not flag:
         sqlite = Sqlite(address + 'ip.db')
         sqlite.update_data('delete from ip_house where id = {}'.format(
             marks[0]))
         Log.d("删除无效代理:" + marks[1])
示例#11
0
文件: Peer.py 项目: mapa17/Eruliaf
 def runOU(self, nSlots, TTL):
     Log.pLD(self, "Executing OU Algorithm for {0}".format(nSlots) )
     #print("[{0}] peers [ {1} ]".format(self.pid, self._peersConn))
    
     #t = SSimulator().tick
     chosen = list()
    
     #Calculate the number of current OU Slots and possible candidates that are interested but not in OU or TFT
     candidates = self.getOUCandidates()
     if(len(candidates) < nSlots):
         self._getMorePeersFlag = True
         nSlots = len(candidates)
         if(nSlots == 0):
             return chosen
     
     candidates = random.sample(candidates, len(candidates)) #Array of peerId
     
     while( len(chosen) < nSlots ):
         p = candidates.pop(0)
         
         #Only unchok peers that are not already unchoked!
         if( (p[4] != self.OU_SLOT) ):
             self._peersConn[p[1]][2].unchock()    
         
         self._peersConn[p[1]] = ( p[0], p[1], p[2], TTL, self.OU_SLOT )
         chosen.append(p[1])
         
     #Do chocking of all OU peers that currently are unchoked but not have been chosen in this round
     for p in self._peersConn.values():
         if( (p[4] == self.OU_SLOT) and (chosen.count(p[1]) == 0) ):
             self._peersConn[p[1]] = ( p[0], p[1], p[2], -1, self.NO_SLOT )
             self._peersConn[p[1]][2].chock()
             
     self._nOUSlots = len(chosen) 
     return chosen
示例#12
0
    def verifyCodeAutoByMyself(self,type=TYPE_LOGIN_NORMAL_WAY):
        try:
            urlInfo = loginUrls['other']['captcha'] if type == TYPE_LOGIN_OTHER_WAY else loginUrls['normal']['captcha']
            Log.v('正在获取验证码...')

            response = EasyHttp.send(urlInfo)
            address = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/image_captcha/'

            byte_stream = BytesIO(response)
            roiImg = Image.open(byte_stream)  # Image打开二进制流Byte字节流数据
            imgByteArr = BytesIO()  # 创建一个空的Bytes对象
            roiImg.save(imgByteArr, format='PNG')  # PNG就是图片格式,我试过换成JPG/jpg都不行
            imgByteArr = imgByteArr.getvalue()  # 这个就是保存的二进制流
            file_name = '1.jpg'
            file_path = address + file_name
            # 下面这一步只是本地测试, 可以直接把imgByteArr,当成参数上传到七牛云
            with open(file_path, "wb") as f:
                f.write(imgByteArr)

            from train.image_captcha import cut_image
            results = cut_image.cut_image(address,file_name)
            results = self.__indexTransCaptchaResults(results)

        except Exception as e:
            Log.w(e)
            return None, False
        return results, self._captchaAutoCheck(results)
示例#13
0
 def freeProxyFifth():
     """
     guobanjia http://www.goubanjia.com/
     :return:
     """
     url = "http://www.goubanjia.com/"
     tree = EasyHttp.getHtmlTree(url)
     if tree is None:
         Log.w('http://www.goubanjia.com无效')
         return []
     proxy_list = tree.xpath('//td[@class="ip"]')
     # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
     # 需要过滤掉<p style="display:none;">的内容
     xpath_str = """.//*[not(contains(@style, 'display: none'))
                                     and not(contains(@style, 'display:none'))
                                     and not(contains(@class, 'port'))
                                     ]/text()
                             """
     for each_proxy in proxy_list:
         try:
             # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
             ip_addr = ''.join(each_proxy.xpath(xpath_str))
             port = each_proxy.xpath(
                 ".//span[contains(@class, 'port')]/text()")[0]
             yield '{}:{}'.format(ip_addr, port)
         except Exception as e:
             pass
示例#14
0
文件: Peer.py 项目: mapa17/Eruliaf
    def _printStats(self):
       
        #Print PeerList
        t = []
        for i in self._peersConn.values():
            t.append( "( {0}@{1}/{2}/{3} {4}|{5}|{6}|{7} , {8}|{9})".format(i[1], \
                            i[2].getUploadRate(), \
                            i[2].getDownloadRate(),  \
                            i[0], \
                            #i[2].getAverageDownloadRate(), \
                            1 if i[2].chocking == True else 0, \
                            1 if i[2].interested == True else 0, \
                            1 if i[2].peerIsChocking() == True else 0, \
                            1 if i[2].peerIsInterested() == True else 0, \
                            i[3], \
                            "N" if i[4] == self.NO_SLOT else "T" if i[4] ==  self.TFT_SLOT else "P" if i[4] ==  self.TFT_POSSIBLE_SLOT else "O", \
                            ) )

        Log.pLI(self, "[{}] DL {}/{} i{} pA{} TFT {}/{}, OU {}/{} , U/D [{}/{}], PeerList {}".format( self.__class__.__name__, \
            self._torrent.getNumberOfFinishedPieces(), \
            self._torrent.getNumberOfPieces(), \
            len(self.piecesQueue), \
            self._pieceAvailability, \
            self._nTFTSlots, self._maxTFTSlots, \
            self._nOUSlots, self._maxOUSlots, \
            self._uploadRateLastTick, self._downloadRateLastTick,  \
            t ) )
示例#15
0
文件: Login.py 项目: lchwindy/12306
 def isLogin(self):
     formData = {'_json_att': ''}
     jsonRet = EasyHttp.send(self._urlInfo['checkUser'])
     Log.d('checkUser: %s' % jsonRet)
     return jsonRet['data'][
         'flag'] if jsonRet and 'data' in jsonRet and 'flag' in jsonRet[
             'data'] else False
示例#16
0
文件: Query.py 项目: zhubaxia/12306
    def loopQuery(trainDate,
                  fromStation,
                  toStation,
                  passengerType=PASSENGER_TYPE_ADULT,
                  trainsNo=[],
                  seatTypes=[SEAT_TYPE[key] for key in SEAT_TYPE],
                  PASSENGERS_ID=[],
                  leave_period=[],
                  POLICY_BILL=1,
                  timeInterval=QUERY_TICKET_REFERSH_INTERVAL):
        count = 0
        base_query_url = queryUrls['query']['url']
        while True:
            nowTime, status = deadline.do_fix_time()
            if status:
                Log.v('当前时间:%s 处于23点到6点之间,12306处于维护状态,暂不处理下单业务' % nowTime)
                continue

            count += 1
            Log.v('正在为您第%d次刷票' % count + ',当前时间为:%s' %
                  datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            for ticketDetails in Query.querySpec(count, base_query_url,
                                                 trainDate, fromStation,
                                                 toStation, passengerType,
                                                 trainsNo, seatTypes,
                                                 PASSENGERS_ID, leave_period,
                                                 POLICY_BILL):
                if ticketDetails:
                    return ticketDetails
            time.sleep(timeInterval)
示例#17
0
    def loopQuery(trainDate, fromStation, toStation, passengerType=PASSENGER_TYPE_ADULT, trainsNo=[],
                  seatTypes=[SEAT_TYPE[key] for key in SEAT_TYPE], PASSENGERS_ID=[], POLICY_BILL=1,
                  timeInterval=QUERY_TICKET_REFERSH_INTERVAL,heart_beat_request_time = 3):
        count = 0
        base_query_url = queryUrls['query']['url']
        while True:
            nowTime, status = deadline.do_fix_time()
            if status:
                Log.v('当前时间:%s 处于23点到6点之间,12306处于维护状态,暂不处理下单业务' % nowTime)
                continue

            count += 1
            #标记是否登录已失效,heart_beat_request_time次发送一次心跳
            flag = True
            if count % heart_beat_request_time == 0:
                status = check_re_login()
                if not status:
                    flag = False
            Log.v('正在为您第%d次刷票' % count + ',当前时间为:%s' % datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            for ticketDetails in Query.querySpec(count, base_query_url, trainDate, fromStation, toStation,
                                                 passengerType, trainsNo, seatTypes,
                                                 PASSENGERS_ID, POLICY_BILL):
                if ticketDetails:
                    return flag,ticketDetails
            time.sleep(timeInterval)
示例#18
0
    def verifyCodeAuto(self):
        try:
            response = EasyHttp.send(autoVerifyUrls['12305'])

            if response['result_code'] != '0':
                return None, False
            img_base64 = response['image']
            # result = eval(response.split("(")[1].split(")")[0]).get("image")
            # img_base64 = result

            body = {'base64': img_base64}
            response = EasyHttp.post_custom(autoVerifyUrls['api'],data=json.dumps(body)).json()
            # response = requests.post(autoVerifyUrls['api']['url'],json=body,headers ={
            #     'Content-Type': 'application/json',
            # }).json()

            if response['success'] != True:
                return None, False
            body = {
                'check': response['data']['check'],
                'img_buf': img_base64,
                'logon': 1,
                'type': 'D',
                '=':''
            }
            response = EasyHttp.post_custom(autoVerifyUrls['img_url'],data=json.dumps(body)).json()
            content = str(response['res'])
            results = content.replace('(','').replace(')','')
            Log.d('识别坐标:%s'%results)
        except Exception as e:
            Log.w(e)
            return None, False
        return results, self._captchaAutoCheck(results)
示例#19
0
def request_start():
    content_type = request.headers.get('Accept') or ''
    real_ip = request.headers.get('X-Real-Ip') or ''

    Log.info(request.path+' '+format_args(request.args)\
                 +' '+real_ip\
                 +' '+content_type)
示例#20
0
    def __waitForOrderId(self):
        Log.v("正在排队获取订单!")
        count = 0
        while True:
            count += 1
            status, msg, waitTime, orderId, errorMsg = self._queryOrderWaitTime(
            )
            if not Utils.check(status, "queryOrderWaitTime: %s" % msg):
                return None
            Log.v("[%d]正在等待订单提交结果..." % count)
            if waitTime < 0:
                if orderId:
                    Log.v("订单提交成功,订单号: %s" % orderId)
                    return orderId
                elif errorMsg:
                    Log.e(errorMsg)
                    return None
            interval = waitTime // 60
            Log.w("未出票,订单排队中...预估等待时间: %s 分钟" %
                  (interval if interval <= 30 else "超过30"))
            if interval > 30:
                time.sleep(60)
            elif interval > 20:
                time.sleep(30)
            elif interval > 10:
                time.sleep(10)
            else:
                time.sleep(3)

        return None
示例#21
0
 def _onOpenNewEditEntity(self, msg):
     if self._currentEntity is not None:
         if self._currentEntity.getName() == msg.entityName:
             return
         if not self._askToSaveEntity():
             return
     loader = self._app._editorNative.getEntityLoader()
     try:
         newEntity = loader.loadEntity(msg.entityName)
     except Exception as e:
         Log.error(
             "[_EventManager:_onOpenNewEditEntity] Can't load entity '{0}' (Exception: {1})"
             .format(msg.entityName, e))
         traceback.print_tb(e.__traceback__)
         return
     if newEntity is None:
         return
     if not newEntity.loadToNative():
         Log.error(
             "[_EventManager:_onOpenNewEditEntity] Can't load entity '{0}' to native"
             .format(msg.entityName))
         return
     if self._currentEntity is not None:
         self._currentEntity.unloadFromNative()
     self._currentEntity = newEntity
     SendMessage(MsgSetEditEntity(self._currentEntity))
示例#22
0
def request_start():
    content_type = request.headers.get('Accept') or ''
    real_ip = request.headers.get('X-Real-Ip') or ''

    Log.info(request.path+' '+format_args(request.args)\
                 +' '+real_ip\
                 +' '+content_type)
示例#23
0
 def onEntityDoubleClickFromFileTree(self, entityName):
     if self._currentEntity is not None:
         if self._currentEntity.getName() == entityName:
             return
         if not self._askToSaveEntity():
             return
     loader = self._app._editorNative.getEntityLoader()
     try:
         newEntity = loader.loadEntity(entityName)
     except Exception as e:
         Log.error(
             "[_EventManager:onEntityDoubleClickFromFileTree] Can't load entity '{0}' (Exception: {1})"
             .format(entityName, e))
         return
     if newEntity is None:
         return
     if not newEntity.loadToNative():
         Log.error(
             "[_EventManager:onEntityDoubleClickFromFileTree] Can't load entity '{0}' to native"
             .format(entityName))
         return
     if self._currentEntity is not None:
         self._currentEntity.unloadFromNative()
     self._currentEntity = newEntity
     self._currentEntity._syncWithNative()
     GetMainViewManager().onEntityDoubleClickFromFileTree(
         self._currentEntity)
示例#24
0
文件: Login.py 项目: Faiz56/12306
    def _loginAsyncSuggest(self, userName, userPwd, autoCheck=2):
        self._init()
        if autoCheck == CAPTCHA_CHECK_METHOD_THREE:
            results, verify = Captcha().verifyCodeAuto()
        elif autoCheck == CAPTCHA_CHECK_METHOD_HAND:
            results, verify = Captcha().verifyCaptchaByHand()
        else:
            results, verify = Captcha().verifyCodeAutoByMyself()
        if not verify:
            return False, '验证码识别错误!'
        Log.v('验证码识别成功')
        formData = {
            'loginUserDTO.user_name': userName,
            'userDTO.password': userPwd,
            'randCode': results,
        }
        jsonRet = EasyHttp.send(self._urlInfo['login'], data=formData)
        # print('loginAsyncSuggest: %s' % jsonRet)

        def isSuccess(response):
            return response['status'] and response['data'].get('loginCheck') == 'Y' if 'data' in response else False, \
                   response['data'].get('otherMsg') if 'data' in response else response['messages']

        loginSuccess, otherMsg = isSuccess(jsonRet)
        return loginSuccess, '%s:%s' % (userName, '登录成功' if loginSuccess else '登录失败')
示例#25
0
 def querySpec(flag,
               base_url,
               trainDate,
               fromStation,
               toStation,
               passengerType=PASSENGER_TYPE_ADULT,
               trainsNo=[],
               seatTypes=[SEAT_TYPE[key] for key in SEAT_TYPE],
               PASSENGERS_ID=[],
               POLICY_BILL=1):
     for ticket in Query.query(flag, base_url, trainDate, fromStation,
                               toStation, passengerType):
         # filter trainNo
         if not TrainUtils.filterTrain(ticket, trainsNo):
             continue
         # filter seat
         for seatTypeName, seatTypeProperty in TrainUtils.seatWhich(
                 seatTypes, ticket):
             if seatTypeProperty and seatTypeProperty != '无':
                 Log.v('%s %s: %s' %
                       (ticket.trainNo, seatTypeName, seatTypeProperty))
                 try:
                     remind_num = int(seatTypeProperty)
                 except Exception as e:
                     remind_num = 100
                 if POLICY_BILL == POLICY_BILL_ALL and len(
                         PASSENGERS_ID) > remind_num:
                     break
                 ticket.seatType = SEAT_TYPE[seatTypeName]
                 ticket.remindNum = remind_num
                 yield ticket
     return []
示例#26
0
    def showSubmitInfoPretty(self):
        status, msg, jsonTicketInfo = self._queryMyOrderNoComplete()
        if not Utils.check(status, msg):
            return False
        from prettytable import PrettyTable
        table = PrettyTable()
        table.field_names = '序号 车次信息 席位信息 旅客信息 票款金额 车票状态'.split(sep=' ')
        totalTicketNum = TrainUtils.submitTicketTotalNum(jsonTicketInfo)
        rows = []
        for i in range(totalTicketNum):
            row = [
                i + 1,
                TrainUtils.submitTrainInfo(i, jsonTicketInfo),
                TrainUtils.submitCoachInfo(i, jsonTicketInfo),
                TrainUtils.submitPassengerInfo(i, jsonTicketInfo),
                TrainUtils.submitTicketCostInfo(i, jsonTicketInfo),
                TrainUtils.submitTicketPayInfo(i, jsonTicketInfo),
            ]
            rows.append(row)
        for row in rows:
            table.add_row(row)

        print(table)
        EmailUtils.send(EmailUtils.buildHTML(rows), 'html')
        Log.v('总张数:%d\t待支付金额:%s' % (totalTicketNum, Fore.YELLOW + '{}元'.format(
            TrainUtils.submitTicketTotalCost(jsonTicketInfo)) + Fore.RESET))
        return True
示例#27
0
    def _setItemForPreview(self, item):
        if item.__class__ != FileNode:
            self._clearPreview()
            return

        path = item.getFullPath()

        fileSize = os.path.getsize(path)
        self._sizeInfo.show()
        self._sizeInfo.setText("<b>Size:</b> {0}".format(
            _convertSize(fileSize)))

        if item._type == FileNodeType.Image:
            image = QPixmap(path)
            if image.isNull():
                self._dimInfo.hide()
                self._previewLabel.setText("<b>No Preivew</b>")
                Log.error(
                    "[FilePreview:_setItemForPreview] Can't load image: '{0}'".
                    format(path))
            else:
                self._dimInfo.show()
                self._dimInfo.setText("<b>Dimension:</b> {0}x{1}".format(
                    image.width(), image.height()))
                self._scaleFactor = 1
                self._previewLabel.setPixmap(
                    _scalePixmap(image,
                                 self._scrollArea.width() - 2,
                                 self._scrollArea.height() - 2))
                self._previewLabel.adjustSize()
        else:
            self._clearPreview()
示例#28
0
文件: Capthca.py 项目: ykbj/12306
    def VerifyCodeAuto(self, type=TYPE_LOGIN_NORMAL_WAY):
        url_12305 = 'https://kyfw.12306.cn/passport/captcha/captcha-image64?login_site=E&module=login&rand=sjrand&'.format(
            random.random())
        try:
            response = EasyHttp.send(autoVerifyUrls['12305'])

            if response['result_code'] != '0':
                return None, False
            img_base64 = response['image']

            body = {'base64': img_base64}
            response = EasyHttp.send(autoVerifyUrls['api'], json=body)

            if response['success'] != True:
                return None, False
            body = {
                'check': response['check'],
                'img_buf': img_base64,
                'logon': 1,
                'type': 'D'
            }
            response = requests.post(autoVerifyUrls['img_url']['url'],
                                     json=body).json()
            content = str(response['res'])
            results = content.replace('(', '').replace(')', '')

        except Exception as e:
            Log.w(e)
            return None, False
        return results, self._captchaAutoCheck(results)
示例#29
0
文件: Peer.py 项目: mapa17/Eruliaf
    def updateLocalConnectionState(self):   
        self._activeDownloadBandwidthUsage = 0

        if(self._leaveNextRound == True):
            self._torrent.tracker.remPeer(self)
            self._disconnectConnections()
            self.removeSimElement()
            Log.pLI(self, "Leaving torrent ...")
            
        if self._torrent.isFinished() == True :
            if( self._downloadEnd == -1):
                self._downloadEnd = SSimulator().tick
            #So check if we want to leave or stay as a seeder
            if( self._leaveTorrent() == True ):
                self._leaveNextRound = True #Leave next round, if not, the peer wont be seen by the observer as finished!
        else:
            if( (SSimulator().tick >= self._nextPieceQueueUpdate)  or (len(self.piecesQueue) == 0) ):
                Log.pLD(self, "Getting new piece list for downloading ..." )
                (self.piecesQueue, self._pieceAvailability) = self.pieceSelection(self._pieceRandomizeCount) #Simulates the queuing of piece requests to other peers
                self._nextPieceQueueUpdate = SSimulator().tick + 5 #Shedule next update
        
        #Call updateLocalState() on each connection and update their averageDownloadRate
        finishedPieces = self._torrent.getFinishedPieces()
        for i in self._peersConn.values() :
            i[2].updateLocalState(finishedPieces)            
示例#30
0
    def showSubmitInfoPretty(self):
        status, msg, jsonTicketInfo = self._queryMyOrderNoComplete()
        if not Utils.check(status, msg):
            return False
        from prettytable import PrettyTable

        table = PrettyTable()
        table.field_names = "序号 车次信息 席位信息 旅客信息 票款金额 车票状态".split(sep=" ")
        totalTicketNum = TrainUtils.submitTicketTotalNum(jsonTicketInfo)
        for i in range(totalTicketNum):
            table.add_row([
                i + 1,
                TrainUtils.submitTrainInfo(i, jsonTicketInfo),
                TrainUtils.submitCoachInfo(i, jsonTicketInfo),
                TrainUtils.submitPassengerInfo(i, jsonTicketInfo),
                TrainUtils.submitTicketCostInfo(i, jsonTicketInfo),
                TrainUtils.submitTicketPayInfo(i, jsonTicketInfo),
            ])
            if not i == totalTicketNum - 1:
                table.add_row(
                    [2 * "-", 2 * "-", 2 * "-", 2 * "-", 2 * "-", 2 * "-"])

        print(table)
        Log.v("总张数:%d\t待支付金额:%s" % (
            totalTicketNum,
            Fore.YELLOW + "{}元".format(
                TrainUtils.submitTicketTotalCost(jsonTicketInfo)) + Fore.RESET,
        ))
        return True
示例#31
0
文件: Login.py 项目: zou8944/12306
    def _loginNormal(self, userName, userPwd, autoCheck=2):
        if autoCheck == CAPTCHA_CHECK_METHOD_THREE:
            results, verify = Captcha().verifyCodeAuto()
        elif autoCheck == CAPTCHA_CHECK_METHOD_HAND:
            results, verify = Captcha().verifyCaptchaByHand()
        else:
            results, verify = Captcha().verifyCodeAutoByMyself()

        if not verify:
            return False, '验证码识别错误!'
        Log.v('验证码识别成功')
        payload = OrderedDict()
        payload['username'] = userName
        payload['password'] = userPwd
        payload['appid'] = 'otn'
        payload['answer'] = results

        response = EasyHttp.post_custom(self._urlInfo['login'], data=payload)

        def isLoginSuccess(responseJson):
            return 0 == responseJson['result_code'] if responseJson and 'result_code' in responseJson else False, \
                   responseJson[
                       'result_message'] if responseJson and 'result_message' in responseJson else '登录失败'

        if response.status_code != requests.codes.ok:
            return False, "登录请求被强制重定向,准备重试..."
        result, msg = isLoginSuccess(response.json())
        if not result:
            return False, msg
        self._userLogin()
        self._passportRedirect()
        result, msg, apptk = self._uamtk()
        if not Utils.check(result, msg):
            return False, 'uamtk failed'
        return self._uamauthclient(apptk)
示例#32
0
    def __waitForOrderId(self):
        Log.v('正在排队获取订单!')
        count = 0
        while True:
            count += 1
            status, msg, waitTime, orderId, errorMsg = self._queryOrderWaitTime()
            if not Utils.check(status, 'queryOrderWaitTime: %s' % msg):
                return None
            Log.v('[%d]正在等待订单提交结果...' % count)
            if waitTime < 0:
                if orderId:
                    Log.v('订单提交成功,订单号: %s' % orderId)
                    return orderId
                elif errorMsg:
                    Log.e(errorMsg)
                    time.sleep(3)
                    return None
                else:
                    time.sleep(3)
                    continue
            interval = waitTime // 60
            Log.w('未出票,订单排队中...预估等待时间: %s 分钟' % (interval if interval <= 30 else '超过30'))
            if interval > 30:
                time.sleep(60)
            elif interval > 20:
                time.sleep(30)
            elif interval > 10:
                time.sleep(10)
            else:
                time.sleep(3)

        return None
示例#33
0
 def validUsefulProxy(params):
     """
     检验代理是否可用
     """
     marks = params.split('&&')
     if isinstance(marks[1], bytes):
         marks[1] = marks[1].decode('utf8')
     if 'HTTP' == marks[2]:
         proxies = {"http": "http://{proxy}".format(proxy=marks[1])}
     else:
         proxies = {"https": "https://{proxy}".format(proxy=marks[1])}
     flag = None
     try:
         # 超过3秒的代理就不要了
         r = requests.get('http://httpbin.org/ip',
                          proxies=proxies,
                          timeout=3,
                          verify=False)
         if r.status_code == 200 and r.json().get("origin"):
             # print('请求IP:'+r.json().get("origin")+', 代理为:'+json.dumps(proxies))
             Log.v('有效代理:' + marks[1])
             flag = True
     except Exception as e:
         flag = False
     if not flag:
         sqlite = Sqlite(address + 'ip.db')
         sqlite.update_data('delete from ip_house where id = {}'.format(
             marks[0]))
         Log.d("无效代理:" + marks[1])
示例#34
0
    def showSubmitInfoPretty(self):
        status, msg, jsonTicketInfo = self._queryMyOrderNoComplete()
        if not Utils.check(status, msg):
            return False,msg
        from prettytable import PrettyTable
        table = PrettyTable()
        try:
            table.field_names = '序号 车次信息 席位信息 旅客信息 票款金额 车票状态'.split(sep=' ')
            totalTicketNum = TrainUtils.submitTicketTotalNum(jsonTicketInfo)
            results = []
            for i in range(totalTicketNum):
                table.add_row([i + 1,
                               TrainUtils.submitTrainInfo(i, jsonTicketInfo),
                               TrainUtils.submitCoachInfo(i, jsonTicketInfo),
                               TrainUtils.submitPassengerInfo(i, jsonTicketInfo),
                               TrainUtils.submitTicketCostInfo(i, jsonTicketInfo),
                               TrainUtils.submitTicketPayInfo(i, jsonTicketInfo),
                               ])
                results.append([i + 1,
                               TrainUtils.submitTrainInfo(i, jsonTicketInfo),
                               TrainUtils.submitCoachInfo(i, jsonTicketInfo),
                               TrainUtils.submitPassengerInfo(i, jsonTicketInfo),
                               TrainUtils.submitTicketCostInfo(i, jsonTicketInfo),
                               TrainUtils.submitTicketPayInfo(i, jsonTicketInfo),
                               ])
                if not i == totalTicketNum - 1:
                    table.add_row([2 * '-', 2 * '-', 2 * '-', 2 * '-', 2 * '-', 2 * '-'])

            print(table)

            Log.v('总张数:%d\t待支付金额:%s' % (
                totalTicketNum, Fore.YELLOW + '{}元'.format(TrainUtils.submitTicketTotalCost(jsonTicketInfo)) + Fore.RESET))
        except BaseException as e:
            return False,None
        return True,results
示例#35
0
 def unchock(self, uploadRate=-1):
     if(uploadRate > -1):
         self._maxUploadRate = int(uploadRate)
     #if(downloadRate > -1):
     #    self._maxDownloadRate = int(downloadRate)
     self.chocking = False
     Log.pLD(self._srcPeer, "unchocking [{0}@{1}]".format(self._destPeer.pid, self._maxUploadRate) )
     self.__calculateUploadRate()
示例#36
0
def post(session, url, data=None, json=None, **kwargs):
    try:
        response = session.post(url, data, json, **kwargs)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        return response
    except:
        Log.e('can not visit url: ' + url)
        return None
示例#37
0
def normalGet(url, params=None, **kwargs):
    try:
        response = requests.get(url, params=params, **kwargs)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        return response
    except:
        Log.e('can not visit url: ' + url)
        return None
示例#38
0
def zhihu():
    log = Log() 
    startTime = time.time()
    searchUrl = request.form["url"]
    test = zhihuAnj(searchUrl)
    ret = test.start();  
    brain = Brain(ret)
    results = brain.getAll()
    log.i("url:"+searchUrl + "  getAll Time:"+str(time.time()-startTime))
    return results
示例#39
0
文件: Peer.py 项目: mapa17/Eruliaf
    def runSeederOU(self, nRepeatingSlots, nRandomSlots, TTL):
        Log.pLD(self, "Executing Seeder OU Algorithm for {} reapting, and {} ramdon Slots".format(nRepeatingSlots, nRandomSlots) )
       
        #t = SSimulator().tick
        chosen = list()
       
        #Calculate the number of current OU Slots and possible candidates that are interested but not in OU or TFT
        candidates = self.getOUCandidates()
        if(len(candidates) == 0):
            return chosen
        
        #Shuffle candidates to select nRandomSlots really random
        random.shuffle(candidates)
        
        candidates2 = list(candidates)
        
        #Get nRepeatingSlots; Only peers that we are already uploading data to!
        while( (len(chosen) <  nRepeatingSlots) and (len(candidates) > 0) ):
            p = candidates.pop(0)
                       
            #Only get already OU allocated ones
            if( (p[4] != self.OU_SLOT) ):
                continue

            #Skip un interested ones
            if( p[2].peerIsInterested() == False):
                continue 
            
            self._peersConn[p[1]] = ( p[0], p[1], p[2], TTL, self.OU_SLOT )
            chosen.append(p[1])

        #No randomly select nRandomSlots more ( or if we could not select enough nRepeatingSlots, select a random one too )
        candidates = candidates2          
        while( (len(chosen) <  nRepeatingSlots+nRandomSlots) and (len(candidates) > 0) ):
            p = candidates.pop(0)
            
            if(p in chosen):
                continue
            
            #Only unchok peers that are not already unchoked!
            if( (p[4] != self.OU_SLOT) ):
                self._peersConn[p[1]][2].unchock()    
            
            self._peersConn[p[1]] = ( p[0], p[1], p[2], TTL, self.OU_SLOT )
            chosen.append(p[1])
            
        #Do chocking of all OU peers that currently are unchoked but not have been chosen in this round
        for p in self._peersConn.values():
            if( (p[4] == self.OU_SLOT) and (chosen.count(p[1]) == 0) ):
                self._peersConn[p[1]] = ( p[0], p[1], p[2], -1, self.NO_SLOT )
                self._peersConn[p[1]][2].chock()
                
        self._nOUSlots = len(chosen) 
        return chosen
示例#40
0
def index():
    log = Log()
    if request.method == 'POST':
        startTime = time.time()
        url = request.form["url"]
        user = User(url)
        results = user.start()      
        log.i("url:"+url + "  getUserInfo Time:"+str(time.time()-startTime))
        return render_template('index.html',results = results)
    else:
        log.i("IP:"+str(request.remote_addr)+" Web Browse:"+str(request.user_agent))
        return render_template('index.html')
示例#41
0
    def render(self, data, headers =  {}, status_code  = 200, raw = False):

        if request.headers['Accept'] == 'application/xml':
            if raw:
                data = data
            else:
                data = serialize(data, 'xml')
            response = Response(data, content_type='application/xml; charset=utf-8')

        elif request.headers['Accept'] == 'text/csv':
            if raw:
                data = data
            else:
                data = serialize(data, 'csv')

            response = Response(data, content_type='text/csv; charset=utf-8')


        # JSON or invalid Content-Type
        else :
            if raw:
                data = data
            else:
                if self.to_hal is not None and status_code == 200:
                    if 'X-Total-Count' in headers:
                        args = {'total' : headers['X-Total-Count']}
                    else:
                        args = {}
                        
                    data = self.to_hal(data, args = args)

                data = serialize(data, 'json')

            response = Response(data, content_type='application/hal+json; charset=utf-8')


        access_control_headers =  "Content-Length, Content-Type, "
        access_control_headers += "Date, Server, "
        access_control_headers += "X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, "
        access_control_headers += "X-Total-Count"

        headers['Access-Control-Expose-Headers'] = access_control_headers

        response.headers.extend(headers)
        response.status_code = status_code
        real_ip = request.headers.get('X-Real-Ip')

        if real_ip == None:
            real_ip = ''

        Log.info(request.path+' '+real_ip+' '+str(status_code) + ' '+str(response.headers.get('Content-Length')))

        return response
示例#42
0
    def spawnPeers(self):
        if SSimulator().tick == 0:
            Log.w(Log.DEBUG, "Creating new peers ...")
            for i in range(0, int(SConfig().value("nInitialPeers", "Peer"))):
                self.addPeer()

            for i in range(0, int(SConfig().value("nInitialPeers", "PeerC1"))):
                self.addPeer_C1()

        else:
            if self.spawnPeer() == True:
                self.addPeer()

            if self.spawnPeer_C1() == True:
                self.addPeer_C1()
示例#43
0
文件: Peer.py 项目: mapa17/Eruliaf
 def connectToPeer(self, peer):
     #logging.log(Simulator.DEBUG, "[{0}] External peer is connecting! [{1}]".format(self.pid, peer.pid))
     Log.pLD(self, "External peer is connecting! [{0}]".format(peer.pid) )
     newConnection = None
         
     #If we already now this peer, return current connection
     if peer.pid in self._peersConn :
         newConnection = self._peersConn[peer.pid][2]
     else:
         Log.pLD(self, "adding Peer [{0}]".format(peer.pid)) 
         newConnection = Connection(self, peer)
         self._peersConn[peer.pid] = ( 0, peer.pid, newConnection, -1 , self.NO_SLOT)
         newConnection.setUploadLimit( self._calculateUploadRate(newConnection) )
         #newConnection.setDownloadLimit( self._calculateDownloadRate(newConnection) )
         self._peersConn[peer.pid][2].connect()
     
     #Return the connection reference
     return newConnection
示例#44
0
class zhihuAnj():

    def __init__(self,url):
        self.m_url = url
        self.m_ret = dict()
        self.m_log = Log()
        
    def getHtml(self,url):
#         url = "http://www.zhihu.com/answer/23966524"
#         url = self.url + "/voters_profile"
        html = urllib.urlopen(url).read()
        return html;
    
    def getInfo(self,url):
        #
        html = self.getHtml(url);
        print html
        jsonRet = json.loads(html);
        #
        total = jsonRet['paging']['total']
        #
        if int(total)>10:
            next = jsonRet['paging']['next']
            follows = next.split('follows=')
            print follows[1]
            return total,follows[1]
        else:
            return total,None

    
    def getPayLoad(self,total,offset,follows):
        if follows != None:
            url = self.m_url + "/voters_profile?total="+str(total)+"&offset="+str(offset)+"&follows="+follows
        else:
            url = self.m_url + "/voters_profile"
            
        html = self.getHtml(url)
        try:
            jsonRet = json.loads(html)
        except Exception,e:
            self.m_log.e(e)
            return
        payLoad = jsonRet['payload']
        return payLoad
示例#45
0
    def __init__(self):
        self.log = Log()
        self.log.adiciona("Inicializando Aplicação")

        QMainWindow.__init__(self)
        self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())
        self.setupUi(self)

        self.definePropriedades()
        self.defineEventos()
示例#46
0
文件: Peer.py 项目: mapa17/Eruliaf
    def runTFT(self, nSlots, TTL):
        Log.pLD(self, " Executing TFT Algorithm for {0}".format(nSlots) )
        #self._peersConn.sort() #Peer list in the form ( <UploadRate>, <PeerID>, <Connection> ) , sort will on the first field and on collision continue with the others
        
        #t = SSimulator().tick
        chosen = list()
        
        #Now everyone is a candidate, take even current OU Slots, this makes sense to step up a peer from OU to TFT is they are good
        candidates = self.getTFTCandidates()
        
        if(len(candidates) == 0):
            self._getMorePeersFlag = True
            #return chosen #DO NOT RETURN HERE OR THE CHOCKING AT THE END OF THE FUNCTION WONT BE APPLIED
        
        candidates.sort(reverse=True) #Sort candidates based on their uploadRate, (highest uploadRate first)

        shuffledFlag = False
        while( (len(chosen) < nSlots) and (len(candidates) > 0) ):
            p = candidates.pop(0)
            
            #If all the rest of the peers have zero upload, shuffle them
            if((p[0] == 0) and (shuffledFlag == False)):
                shuffledFlag = True #Only shuffle once
                candidates.append(p)
                candidates = random.sample(candidates, len(candidates)) #Array of peerId
                p = candidates.pop(0)
            
            #Only unchok peers that are not already unchoked!
            if( (p[4] != self.TFT_SLOT) ):
                self._peersConn[p[1]][2].unchock()    
            
            self._peersConn[p[1]] = ( p[0], p[1], p[2], TTL, self.TFT_SLOT )
            chosen.append(p[1])
        
        #Do chocking of all TFT peers that currently are unchocked but not have been chosen in this round
        for p in self._peersConn.values():
            if( (p[4] == self.TFT_SLOT) and (chosen.count(p[1]) == 0) ):
                self._peersConn[p[1]] = ( p[0], p[1], p[2], -1, self.NO_SLOT )
                self._peersConn[p[1]][2].chock()
   
        self._nTFTSlots = len(chosen)
        return chosen
class BatchRJ(object):

    def __init__(self, env='dev', log_path=None):
        all_settings=SettingsD8ImportRandosJour()
        self.settings=all_settings.options(env)
        self.champs_date=self.settings['getCrsfTokenRestAction']['url']
        self.log_object=Log(log_path)
        self.rractions=RandoRestAction(self.settings, self.log_object)
    
    
    def importRandosJour(self):
        path_csv = os.path.join(self.settings['pathCsvs'],self.settings['ficCsvRandosCreer']['file'])
        if os.path.exists(path_csv):
            #first we get a token using a simple GET MEthod !!!
            self.rractions.getTokenAndHeaders()
            csvfile = open(path_csv, "r", encoding='utf-8')
            reader = csv.reader(csvfile)
            indice=0
            indice_traite_ok=0
            for csv_row in reader:
                if indice > 0:
                    self.log_object.p("+ on traite la ligne {0}".format(indice))
                    res=self.rractions.createHike(csv_row)
                    if res=="OK":
                        indice_traite_ok+=1
                indice += 1
            self.log_object.p("sur {0} lignes traitees, {1} effectivement inserees".format(indice, indice_traite_ok))
        else:
            self.log_object.p("ERREUR: le fichier {0} est inconnu, Abandon !!!".format(path_csv))
示例#48
0
文件: Peer.py 项目: mapa17/Eruliaf
  def peerLogic(self):
      self._preLogicOperations()
 
      if(self._sleepTime > 0):
          self._sleepTime -= 1
          return
  
      if( (self._runOUFlag == True) or (self._runTFTFlag == True) ):
          printChosen = True
          tftChosen = []
          ouChosen = []
      else:
          printChosen = False
  
      if(self._torrent.isFinished() == False):
          #Leecher
      
          if( self._runTFTFlag == True):
              self._runTFTFlag = False
              nSlots = self._maxTFTSlots
              tftChosen = self.runTFT(nSlots, self._nextTFTPhaseStart)
          
          if( self._runOUFlag == True ):
              self._runOUFlag = False
              nSlots = self._maxOUSlots
              ouChosen = self.runOU(nSlots, self._nextOUPhaseStart)
          
      else:
          #Seeder Part
          if( self._runOUFlag == True ):
              self._runOUFlag = False
              self._nextOUPhaseStart = SSimulator().tick + 10
              ouChosen = self.runSeederOU(4, 1, 10) #Keep 4, get 1 random
              
      if(printChosen == True):
          Log.pLI(self, "TFTChosen {} until {}, OUChosen {} until {}".format( tftChosen, self._nextTFTPhaseStart, ouChosen, self._nextOUPhaseStart ) )
示例#49
0
文件: Peer.py 项目: mapa17/Eruliaf
    def _wakeUpPeer(self):
        
        if( self._sleepTime > 0):
            self._sleepTime -= 1
            return
        
        Log.pLD(self, "Node waking up ...".format())
        
        #Unregister sleep and setup peer for normal operation
        self.unregisterSimFunction(Simulator.ST_INIT, self._wakeUpPeer )
        
        self.registerSimFunction(Simulator.ST_UPDATE_LOCAL, self.updateLocalConnectionState )
        self.registerSimFunction(Simulator.ST_UPDATE_GLOBAL, self.updateGlobalConnectionState )
        self.registerSimFunction(Simulator.ST_LOGIC, self.peerLogic )
        self.registerSimFunction(Simulator.ST_FILETRANSFER, self._runDownloads )
        self.registerSimFunction(Simulator.ST_CONCLUTION, self._conclusionState )

        #Decide when to run tft and ou algorithm next time
        self._nextTFTPhaseStart = SSimulator().tick
        self._nextOUPhaseStart = SSimulator().tick
        self._nextPieceQueueUpdate = SSimulator().tick
        
        #Register to tracker
        self._torrent.tracker.connect(self)
示例#50
0
    def updateGlobalState(self):

        if( self.disconnected == True ):
            #Connection was disconnected, tell peer
            self._srcPeer.peerDisconnect(self)
            return

        #Check if we are seeding , if so, we are __NEVER__ interested
        if(self._srcPeer.getTorrent().isFinished() == False):
        
            #Check what the other peer has to offer
            self._downloadablePieces = self.__calcDownloadablePieceSet()        
            if(len(self._downloadablePieces) > 0):
                Log.pLD(self._srcPeer, "Having interest in {0} from {1}".format( len(self._downloadablePieces), self._destPeer.pid) )
                self.interested = True
                self.__setCurrentPiece()
                #if( self.chocking == True ):
                #    self.unchock(self._uploadRate, 0)
            else:
                Log.pLD(self._srcPeer, "Loosing interest in peer {0}".format(self._destPeer.pid) )
                #self.chocking = True
                self.interested = False
        else:
            self.interested = False
示例#51
0
    def runDownload(self):
        #If nothing happens, nothing is downloaded
        self._downloadRate = 0
        
        #Downloading happens if we are interested and the other peer is not chocking us
        if( (self.interested == True) and (self.remoteConnection.chocking == False) ):

            #Limit maximum download rate
            currentDownloadRate = self.remoteConnection.getUploadRate()
            currentDownloadRate = self._srcPeer.requestDownloadBandwidth(currentDownloadRate)
            #if(currentDownloadRate > self._maxDownloadRate):
            #    currentDownloadRate = self._maxDownloadRate

            #Check if we finished a complete piece and if so mark it as finished and get the next one
            self._acumulatedData += currentDownloadRate
           
            self._downloadRate = currentDownloadRate
            
            #Make this int so we dont get too long floating number in output log
            self._downloadRate = int(self._downloadRate)
            
            Log.pLD(self._srcPeer, "Downloaded {0}/{1} of piece {2}".format(self._acumulatedData, self._srcPeer.getTorrent().pieceSizeBytes, self._currentPiece) )
            while(self._acumulatedData > self._srcPeer.getTorrent().pieceSizeBytes):
                Log.pLD(self._srcPeer, "Finished downloading piece {0}".format(self._currentPiece) )
            
                self._srcPeer.finishedDownloadingPiece(self, self._currentPiece )
                self._acumulatedData -= self._srcPeer.getTorrent().pieceSizeBytes
                self._currentPiece = -1
                #Set a next piece and utilize the data downloaded for this one. If there are no more pieces discard the downloaded data
                if(self.__setCurrentPiece() == False):
                    self._acumulatedData = 0   #This is simulating lost bandwidth due to running out of piece requests
                    Log.pLD(self._srcPeer, "Piece request queue empty!" )
                    #On start of next round we will loose interest
                    #self.interested = False
            
            if(self._currentPiece == -1):
                self.__setCurrentPiece()
示例#52
0
 def __del__(self):
     # close session after query executes
     self.session.close()
     Log.info('Database connection closed')
示例#53
0
class JanelaPrincipal(QMainWindow, Ui_pydatagenMain):
    '''
    Instancia a Janela Principal(e por enquanto única da aplicação)
    If isn't work, it's because something wrong is not right
    '''

    def __init__(self):
        self.log = Log()
        self.log.adiciona("Inicializando Aplicação")

        QMainWindow.__init__(self)
        self.move(QtGui.QApplication.desktop().screen().rect().center() - self.rect().center())
        self.setupUi(self)

        self.definePropriedades()
        self.defineEventos()


    def definePropriedades(self):
        self.log.adiciona("Definindo Propriedades")

        self.mapper = Mapper()

        self.setWindowIcon(QtGui.QIcon('img/icone.png'))

        self.optMySql.setIcon(QtGui.QIcon('img/mysql-icon.png'))
        self.optPostgres.setIcon(QtGui.QIcon('img/postgres-icon.png'))

        #Define a janela como não-redimensionnável
        #self.setFixedSize(self.width(), self.height())

        # self.txtLog.setEnabled(False)

        #define parametros de conexao
        self.txtServidor.setText('127.0.0.1')
        self.txtUsuario.setText('root')
        self.txtSenha.setText('ericktm')
        self.txtBanco.setText('imovel_banco')

        #Força a exibição da primeira aba ao abrir
        self.tabs.setCurrentIndex(0)
        self.tblAtributes.setRowCount(100)
        self.showMaximized()

    def defineEventos(self):
        '''
        Associa os enventos de interface à metodos
        '''
        self.log.adiciona("Definindo Eventos")
        self.connect(self.btnConfig, SIGNAL('clicked()'), self.validaConfig)
        self.connect(self.btnPassAll, SIGNAL('clicked()'), self.validaConfig)
        self.connect(self.optMySql, SIGNAL('clicked()'), self.defineEngine)
        self.connect(self.optPostgres, SIGNAL('clicked()'), self.defineEngine)
        self.connect(self.btnPassOne, SIGNAL('clicked()'), self.addTabela)
        self.connect(self.btnPassAll, SIGNAL('clicked()'), self.addAllTables)
        self.connect(self.btnRetOne, SIGNAL('clicked()'), self.removeTable)
        self.connect(self.btnRetAll, SIGNAL('clicked()'), self.removeAllTables)
        self.connect(self.btnTables, SIGNAL('clicked()'), self.updateTables)
        self.connect(self.cboTable, SIGNAL('currentIndexChanged(QString)'), self.updateProperties)
        # self.connect(self.cboField,SIGNAL('currentIndexChanged(QString)'),self.updateProperties)

    def teste(self):
        QMessageBox.about(self, "Campo Obrigatório!", "teste!")
        print 'teste'

    def validaConfig(self):
        '''
        Valida as configurações de Conexão ao banco de dados inseridas na primeira aba.
        '''
        self.addLog('Validando parâmetros de conexão')
        servidor = str(self.txtServidor.text())
        usuario = str(self.txtUsuario.text())
        senha = str(self.txtSenha.text())
        banco = str(self.txtBanco.text())
        sgbd = self.defineEngine()

        if servidor != "":
            if usuario != "":
                if senha != '':
                    if banco != '':
                        self.addLog('Tentando Autenticar ao banco de dados')
                        self.mapper.conecta(sgbd, servidor, usuario, senha, banco)
                        self.updatelstTables()
                    else:
                        QMessageBox.about(self, "Campo Obrigatório!", "Preencha o banco de dados!")
                        self.txtBanco.setFocus()
                else:
                    QMessageBox.about(self, "Campo Obrigatório!", "Preencha o campo senha!")
                    self.txtSenha.setFocus()
            else:
                QMessageBox.about(self, "Campo Obrigatório!", "Preencha o campo usuário!")
                self.txtUsuario.setFocus()
        else:
            QMessageBox.about(self, "Campo Obrigatório!", "O campo servidor é obrigatório!")
            self.txtServidor.setFocus()

    def defineEngine(self):
        if self.optMySql.isChecked():
            self.addLog('Banco de Dados: Mysql')
            engine = 'mysql+mysqldb'
        else:
            self.addLog('Banco de Dados: PostgreSQL')
            engine = 'postgresql'
        return str(engine)

    def updateTitle(self, texto=None):
        self.addLog('Atualizando Título da página')
        self.setWindowTitle(texto)

    def addLog(self, text):
        self.log.adiciona(str(text))
        self.txtLog.setHtml(self.log.trace)

    def updatelstTables(self):
        self.lstDipTables.clear()
        for tabela in self.mapper.map['tabelas']:
            self.lstDipTables.addItem(tabela)

    def addTabela(self):
        item = self.lstDipTables.takeItem(self.lstDipTables.currentRow())
        self.lstTabsSelec.addItem(item)
        item = None

    def removeTable(self):
        item = self.lstTabsSelec.takeItem(self.lstTabsSelec.currentRow())
        self.lstDipTables.addItem(item)
        item = None

    def removeAllTables(self):
        self.addLog('Removendo todas as tabelas selecionadas')
        for index in xrange(self.lstTabsSelec.count()):
            self.lstDipTables.addItem(self.lstTabsSelec.item(index).text())
        self.lstTabsSelec.clear()
        self.addLog('Lista de tabelas selecionadas limpa')

    def addAllTables(self):
        self.addLog('Adicionando todas as tabelas disponíveis para inserção')
        for index in xrange(self.lstDipTables.count()):
            self.lstTabsSelec.addItem(self.lstDipTables.item(index).text())
        self.lstDipTables.clear()
        self.addLog('Lista de tabelas disponíveis limpa')

    def updateTables(self):
        self.addLog('Limpando combo de tabelas selecionadas')
        self.cboTable.clear()
        if self.lstTabsSelec.count() > 0:
            self.addLog('Atualizando combo de tabelas selecionadas')
            for i in xrange(self.lstTabsSelec.count()):
                self.cboTable.addItem(self.lstTabsSelec.item(i).text())
        else:
            self.addLog('Nenhuma tabela selecionada')
            QMessageBox.about(self, "Campo Obrigatório!", "Nenhuma tabela selecionada!")
            self.tabs.setCurrentIndex(1)

    def updateFields(self):
        self.addLog('Atualizando propriedades')
        #--- Implementar

    def cleanTable(self):
        '''
        Método responsável por limpar a tabela de propriedades
        '''
        self.addLog(u"Atualizando propriedades de tabela")
        #Altera  quantidade de colunas
        self.tblAtributes.setRowCount(1)

        #Altera a Quantidade de linhas
        self.tblAtributes.setColumnCount(5)
        #Define nome das colunas da tabela
        self.tblAtributes.setHorizontalHeaderItem(0,QTableWidgetItem(u"Nome do Campo"))
        self.tblAtributes.setHorizontalHeaderItem(1,QTableWidgetItem(u"Primário?"))
        self.tblAtributes.setHorizontalHeaderItem(2,QTableWidgetItem(u"Nulo?"))
        self.tblAtributes.setHorizontalHeaderItem(3,QTableWidgetItem(u"Tipo de Dado"))
        self.tblAtributes.setHorizontalHeaderItem(4,QTableWidgetItem(u"Opções"))

    def updateProperties(self):

        '''
        Método responsável por atualizar a tabela de propriedades(Conforme as propriedades
        mapeadas) com a tabela selecionada no
        momento no campo cboTables.

        '''

        #Limpa a tabela
        self.cleanTable()
        #Captura a tabela selecionada
        texto = str(self.cboTable.currentText())
        #Recupera as informações mapeadas referente à tabela selecionada
        tabela = self.mapper.map['tabelas'][texto]
        self.tblAtributes.setRowCount(len(tabela))
        i = 0
        for chave, valor in tabela.items():

            #Atualiza Tabela
            self.tblAtributes.setItem(i, 0, QTableWidgetItem(chave))
            self.tblAtributes.setItem(i, 1, QTableWidgetItem(valor['primario']))
            self.tblAtributes.setItem(i, 2, QTableWidgetItem(valor['nulo']))
            self.tblAtributes.setItem(i, 3, QTableWidgetItem(valor['tipo']))
            self.tblAtributes.setItem(i, 4, QTableWidgetItem("Customizar--"))
            i+=1

        item = QTableWidgetItem()
示例#54
0
 def addPeer_C1(self):
     uploadRate = random.randint(self.C1uMin, self.C1uMax)
     downloadRate = random.randint(self.C1dMin, self.C1dMax)
     sleep = random.randint(0, self._C1maxSleep)
     p = Peer_C1(Torrent(self.__tracker), uploadRate, downloadRate, sleep)  # Create new peer
     Log.w(Log.INFO, "New Peer_C1 {} Up/Down [{}/{}] Sleep {}".format(p.pid, uploadRate, downloadRate, sleep))
示例#55
0
文件: Peer.py 项目: mapa17/Eruliaf
 def __del__(self):
     Log.pLD("Peer is being destroyed")
示例#56
0
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from urls import *
import argparse
import logging
from utils.Log import Log


parser = argparse.ArgumentParser()

parser.add_argument('--dev', action="store_true", default=False)

app.config['PROPAGATE_EXCEPTIONS'] = True

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

if __name__ == '__main__':
    arguments = parser.parse_args()
    if arguments.dev:
        app.config['DEBUG'] = True
        Log.info("Running webserver in DEBUG mode")
        app.run(host = app.config['WEBSERVER_ADDR'], port = app.config['WEBSERVER_PORT'])
    else:
        Log.info("Running webserver in DEPLOYMENT mode")
        sockets = tornado.netutil.bind_sockets(app.config['WEBSERVER_PORT'])
        tornado.process.fork_processes(app.config['SUBPROCESS_NUMBER'])
        server = HTTPServer(WSGIContainer(app))
        server.add_sockets(sockets)
        IOLoop.current().start()
示例#57
0
文件: Eruliaf.py 项目: mapa17/Eruliaf
    #Load Config
    SConfig.setPath( sys.argv[1] )
    C = SConfig()
    
    print("Starting Simulation with config {0}".format(sys.argv[1] ))
    #import time
    #time.sleep(5)
    random.seed(int(SConfig().value("randSeed")))
    newLog = Log() #Create this log for the whole project
    logging.log(Log.INFO, "Starting simulation") 
    
    S = SSimulator()
    T = Tracker()
    O = Observer( T )    
    
    logging.log(Log.INFO, "Creating nodes ...")
        
    tor = Torrent( T )
    Log.w(Log.INFO, "Piece size [{0}] bytes".format(tor.pieceSizeBytes) )
    s = Seeder( tor , SConfig().value("SeederUpload"), SConfig().value("SeederDownload") )
    
    logging.log(Log.INFO, "Creating {0}".format(s))
    
    pF = PeerFactory(T)

    S.start()

    logging.log(Log.INFO, "Ending simulation")    
    
    sys.exit(0)
 def __init__(self, env='dev', log_path=None):
     all_settings=SettingsD8ImportRandosJour()
     self.settings=all_settings.options(env)
     self.champs_date=self.settings['getCrsfTokenRestAction']['url']
     self.log_object=Log(log_path)
     self.rractions=RandoRestAction(self.settings, self.log_object)
示例#59
0
 def chock(self):
     self.chocking = True
     self._uploadRate = 0
     Log.pLD(self._srcPeer, "chocking [{0}]".format(self._destPeer.pid) )