Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

screenAgent.py #20

Open
margaretmm opened this issue Jul 13, 2018 · 0 comments
Open

screenAgent.py #20

margaretmm opened this issue Jul 13, 2018 · 0 comments

Comments

@margaretmm
Copy link
Owner

-- coding: UTF-8 --

import requests
import os
import datetime
import tarfile
import fnmatch
from queue import Queue
from PIL import ImageGrab
import time
import schedule
import threading
import logging
import logging.config

log_filename = "screenAgent.log"
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s] %(levelname)s [%(funcName)s: %(filename)s, %(lineno)d] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filemode='a')

#pip install -i https://pypi.doubanio.com/simple/ schedule
class tarFileTransfer():
def init(self,desIP,desPort):
self.gQueue = Queue() # 创建队列对象
self.gFileDir = ".\test\"
self.gRarDir = ".\"
self.desIP=desIP
self.desPort=desPort

def imgCrab(self):
    ct = int(time.time())
    # print(ct)
    a = ImageGrab.grab()
    a.save(self.gFileDir + str(ct) + '.jpg')
    #self.gQueue.put(ct)

def count_file(self,path):
    count=0
    ls = os.listdir(path)
    for i in ls:
        count+=1
    return count

def del_file(self,path):
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)
        if os.path.isdir(c_path):
            self.del_file(c_path)
        else:
            os.remove(c_path)

def find_spe_file(self,root, patterns=['*'], non_cludedir=[]):
    for root, dirnames, filenames in os.walk(root):
        for pattern in patterns:
            for filename in filenames:
                if fnmatch.fnmatch(filename, pattern):
                    #print(filename)
                    yield os.path.join(root, filename)

def cre_tarfile(self,fileDir):
    args = ["*.jpg", "*.jepg"]
    if self.count_file(fileDir)==0:
        return
    now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
    filename = "all_img_{0}.tar.gz".format(now)
    with tarfile.open(filename, mode='w:gz') as f:
        ret=self.find_spe_file(fileDir, args)
        for item in ret:
            #print(item)
            f.add(item)
    if(len(f.members)==0):
        self.del_file('./'+filename)
        return

    ret=1
    count=0
    while (ret>0):
        if (count>=5):
            logging.error("sent file retry times is >5 ")
            break;
        ret=self.tarSend(self.desIP,self.desPort,self.gRarDir+filename)
        if 0==ret:
            #删除已经打包的图片文件
            self.del_file(self.gFileDir)
        count+=1
    return

def tarSend(self,desIP,desPort,rarFile):
    url = 'http://'+ desIP +':'+str(desPort)
    print(rarFile)
    files = {'file': open(rarFile, 'rb')}
    return 0
    '''
    try:
        r = requests.post(url, files=files)
        r.raise_for_status()
        return 0
    except requests.RequestException as e:
        print(e)
        logging.error("sent file to %s exception: %s ",url,e)
        return 1
    else:
        result = r.json()
        print(type(result), result, sep='\n')
        return 2
    '''

def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()

if name == "main":
a = tarFileTransfer('127.0.0.1','9000')

schedule.every(60).seconds.do(run_threaded,a.imgCrab)

schedule.every(3).minutes.do(run_threaded,a.cre_tarfile(a.gFileDir))
while True:
    schedule.run_pending()
    time.sleep(1)
'''
t1 = threading.Thread(target=a.imgCrabIimer, args=(1,))
t1.start()
t1.setDaemon(True)
#a.imgCrabIimer(1)
a.tarSendTimer(10)
t1.join()
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant