python讀取文章的最後一行

用來作個小小的持久化,讀取文檔中最後一行(也多是第一行) @後的部分,其餘網址上面寫的相似的有Bug,因此本身優化了一下python

 

import requests,os,logging
logging.basicConfig(level='DEBUG')
class QingguoProxy():
    def __init__(self,usr,pwd,last_host=None):
        self.usr=usr
        self.pwd=pwd
        self.session=requests.session()
        self.file_name='qingguoip.txt'
        self.last_host=last_host or self.get_last_host()
       
    def get_last_host(self):
        #獲取上次的taskid@host
        if os.path.exists(self.file_name):
            print ('have file_name')
            with open(self.file_name,'rb') as f:
                _line=f.readline().decode()
                offset=-10
                of_type=None
                for i in range (30):
                    if not of_type:
                       of_type=2
                    else:
                       of_type=1
                    index=f.seek(offset,of_type)
                    if index>offset*-1:
                        lines= f.readlines()
                        if len(lines)>=2:
                            last_line=lines[-1]
                            _line=last_line.decode()
                            break
                return _line.split('@')[-1] 
        else:
            logging.info('沒有保存上次的taskid和host,請手動填入last_host的值生產對象')


        
qingguoip.txt內容爲
aaa@BNBBaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1
bb@xxa
c@!
 session

相關文章
相關標籤/搜索