# -*- coding: utf-8 -*- #!/usr/bin/env python # encoding:utf-8 # 注意:import顺序很重要 import re import os, sys # 将django项目根目录加入环境变量 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录 sys.path.append(os.path.abspath(os.path.join(BASE_DIR, os.pardir))) # 引入django配置文件 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yfwlw_pro.settings") # 你的django的settings文件 # 启动django import django django.setup() import requests from PIL import Image from io import BytesIO import datetime import sys from apps.EquipManage.views import CJSONEncoder import json import re from pyinotify import WatchManager, Notifier, \ ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY from apps.AppInfoManage.models import Alarm_record, BZYphoto, Equip, Equip_Forward, Equip_type, MyUser class EventHandler(ProcessEvent): """事件处理""" def process_IN_CREATE(self, event): print ("Create file: %s " % os.path.join(event.path, event.name)) if event.name.endswith(".jpg"): nowtime = datetime.datetime.now().strftime('%Y%m%d') origin = sys.stdout f = open('../logs/'+nowtime+'bzy.txt','a+') sys.stdout = f print("new file upload is ok!") print("type event.path:",type(event.path)) print("event.path:",event.path) print("event.name:",event.name) # imei = re.findall(r"/(.+?)/",event.path) imei = re.findall(r"\d+\.?\d*",event.path)[0] print("imei:", imei) # 判断设备是否存在: if Equip.objects.filter(equip_id=imei).exists(): equip = Equip.objects.get(equip_id=imei) img_addr = "pyftp/" + os.path.join(event.path, event.name) if BZYphoto.objects.filter(addr=img_addr).exists(): pass else: BZYphoto.objects.create(equip_id=equip, addr=img_addr) print("图片增加链接成功!") try: if Equip_Forward.objects.filter(equip_id=imei).exists(): equip_list = Equip_Forward.objects.get(equip_id=imei) # if equip.ftp_addr: # http://120.27.222.26/cbd/pyftp/ftp_file/ykm_cbd/868575028845867/20190305/20:56:15.jpg ftp_addr = equip_list.equip_img_url pattern = r'[#]' split_result = re.split(pattern, ftp_addr) print("split_result:",split_result) try: img_addr_1 = "http://120.27.222.26/" + "bzy/" + img_addr print("img_addr_1:",img_addr_1) data = { "Image":img_addr_1, "imei":imei } data = json.dumps(data, cls=CJSONEncoder) print("data:",data) print("url:",split_result[0]) if equip_list.equip_img_between == "1": res_1 = requests.post(split_result[0],data=data,timeout=2) elif equip_list.equip_img_between == "2": headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'} res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers) print("res_1:",res_1.text) # 判断文件大小的方法 # path = 'F:\YFWLW\yfwlw\\16_19_21.jpg' # size = os.path.getsize(path)/1024 print("------------------对接设备图片发送成功!!!") except Exception as aexc: print(aexc) print("------------------对接设备图片发送失败!!!") except Exception as exce: print(exce) print("!!!") else: print("该设备不存在") sys.stdout = origin f.close() def process_IN_DELETE(self, event): print ("Delete file: %s " % os.path.join(event.path, event.name)) # imei = re.findall(r"\d+\.?\d*",event.path)[0] # img_addr = "pyftp/" + os.path.join(event.path, event.name) # img_addr = "/home/web/yfwlw/" + img_addr[0:-9] # print("image_addr:",img_addr) # print("判断是否存在:",os.path.exists(img_addr)) if event.name.endswith(".jpg"): print("file delete is ok!") # def process_IN_MODIFY(self, event): # print ("Modify file: %s " % os.path.join(event.path, event.name)) def FSMonitor(path='.'): wm = WatchManager() mask = IN_DELETE | IN_CREATE | IN_MODIFY notifier = Notifier(wm, EventHandler()) wm.add_watch(path, mask, auto_add=True, rec=True) print('ftp file watchdog is running') print('now starting monitor %s' % (path)) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break if __name__ == "__main__": FSMonitor('ftp_file/bzy_photo')