Python 查看 QNAP FTP 目录的变化并发送 UDP 消息
Python watch QNAP FTP directory for changes and send UDP messages
我需要编写一个程序来监视 ftp 服务器上的目录,然后发送一条消息,其中包含新文件的路径。所以我确实设法将 watchdog
用于本地文件夹,因为我只需要创建事件:
if __name__ == "__main__":
patterns = "*"
ignore_patterns = ""
ignore_directories = False
case_sensitive = False
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
def on_created(event):
byte_message = bytes(f"{event.src_path}", "utf-8")
opened_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
opened_socket.sendto(byte_message, ('address', port))
print(f"{event.src_path} created")
my_event_handler.on_created = on_created
path = r"local/path"
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
但是当我尝试用 FTP 上的文件夹替换 path
时,我得到 Access Denied
这是正确的,因为我没有设置登录名和密码。之后我根据 Stack Overflow 上的一个答案做了这个:
ftp = FTP()
ftp.set_pasv(True)
ftp.connect("address")
ftp.login('user', 'pass')
def changemon(dir='ftp/path/*'):
ls_prev = set()
while True:
ls = set(ftp.nlst(dir))
add = ls-ls_prev
if add:
yield add
ls_prev = ls
sleep(5)
for add in changemon():
byte_message = bytes('\n'.join(r'address%' % i for i in add), 'utf-8')
opened_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
opened_socket.sendto(byte_message, ('address', port))
问题在于它获取出现在 dir
中的每个文件。
所以我需要一些可以从中提取子文件夹名称和文件名的东西。像这样: file1
和 file2
是新建的:
ftp/path/5000/file1
ftp/path/5001/file2
print('Folder is: ' + foldername)
Folder is: 5000
Folder is: 5001
print('New file is: ' + filename)
New file is: file1
New file is: file2
欢迎任何帮助。
这是我无法从商店中找到任何可以帮助我的解决方案(任务是在 python 中完成):
from ftplib import FTP
from time import sleep
import os
import byte
import socket
import numpy as np
import pandas as pd
ftp = FTP()
ftp.set_pasv(True)
ftp.connect("some-ip-address")
ftp.login('user', 'password')
def changemon(dir='/dir/*'):
ls_prev = set()
while True:
ls = set(ftp.nlst(dir))
add = ls-ls_prev
if add:
yield add
ls_prev = ls
sleep(5)
#tcp
for add in changemon():
result = [i for i in add]
array = np.array(result, dtype='str')
df = pd.DataFrame(array, columns=None, index=None)
for a in array:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
name = a.split('/')
separator = ''
path = (r'ip\dir\' + str(name[2]) + '\' + str(name[3]))
device = name[2]
message = bytes(separator.join(f'{device} desired string for the IP notification \n'), 'utf-8')
s.send(message)
sleep(5)
这不是最漂亮的解决方案,但可以完成工作。斜杠方向相反,因为需要在 Windows 中读取消息。然后 path
变量被插入到数据库中。
我需要编写一个程序来监视 ftp 服务器上的目录,然后发送一条消息,其中包含新文件的路径。所以我确实设法将 watchdog
用于本地文件夹,因为我只需要创建事件:
if __name__ == "__main__":
patterns = "*"
ignore_patterns = ""
ignore_directories = False
case_sensitive = False
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
def on_created(event):
byte_message = bytes(f"{event.src_path}", "utf-8")
opened_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
opened_socket.sendto(byte_message, ('address', port))
print(f"{event.src_path} created")
my_event_handler.on_created = on_created
path = r"local/path"
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
但是当我尝试用 FTP 上的文件夹替换 path
时,我得到 Access Denied
这是正确的,因为我没有设置登录名和密码。之后我根据 Stack Overflow 上的一个答案做了这个:
ftp = FTP()
ftp.set_pasv(True)
ftp.connect("address")
ftp.login('user', 'pass')
def changemon(dir='ftp/path/*'):
ls_prev = set()
while True:
ls = set(ftp.nlst(dir))
add = ls-ls_prev
if add:
yield add
ls_prev = ls
sleep(5)
for add in changemon():
byte_message = bytes('\n'.join(r'address%' % i for i in add), 'utf-8')
opened_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
opened_socket.sendto(byte_message, ('address', port))
问题在于它获取出现在 dir
中的每个文件。
所以我需要一些可以从中提取子文件夹名称和文件名的东西。像这样: file1
和 file2
是新建的:
ftp/path/5000/file1
ftp/path/5001/file2
print('Folder is: ' + foldername)
Folder is: 5000
Folder is: 5001
print('New file is: ' + filename)
New file is: file1
New file is: file2
欢迎任何帮助。
这是我无法从商店中找到任何可以帮助我的解决方案(任务是在 python 中完成):
from ftplib import FTP
from time import sleep
import os
import byte
import socket
import numpy as np
import pandas as pd
ftp = FTP()
ftp.set_pasv(True)
ftp.connect("some-ip-address")
ftp.login('user', 'password')
def changemon(dir='/dir/*'):
ls_prev = set()
while True:
ls = set(ftp.nlst(dir))
add = ls-ls_prev
if add:
yield add
ls_prev = ls
sleep(5)
#tcp
for add in changemon():
result = [i for i in add]
array = np.array(result, dtype='str')
df = pd.DataFrame(array, columns=None, index=None)
for a in array:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
name = a.split('/')
separator = ''
path = (r'ip\dir\' + str(name[2]) + '\' + str(name[3]))
device = name[2]
message = bytes(separator.join(f'{device} desired string for the IP notification \n'), 'utf-8')
s.send(message)
sleep(5)
这不是最漂亮的解决方案,但可以完成工作。斜杠方向相反,因为需要在 Windows 中读取消息。然后 path
变量被插入到数据库中。