99 lines
3.7 KiB
Python
99 lines
3.7 KiB
Python
|
|
import mimetypes
|
||
|
|
import time
|
||
|
|
from watchdog.observers import Observer
|
||
|
|
from watchdog.events import FileSystemEventHandler
|
||
|
|
import http.client
|
||
|
|
|
||
|
|
# 服务器的 IP 地址和端口号
|
||
|
|
server = "192.168.6.11"
|
||
|
|
server_port = 8080
|
||
|
|
# 接口路径
|
||
|
|
# server_url = "/Interface/Result/Create"
|
||
|
|
server_url = "/wanhua-service/equipment/external/checkResult"
|
||
|
|
|
||
|
|
class Watcher:
|
||
|
|
def __init__(self, directory_to_watch):
|
||
|
|
self.observer = Observer()
|
||
|
|
self.directory_to_watch = directory_to_watch
|
||
|
|
|
||
|
|
def run(self):
|
||
|
|
print(f'开始监听文件夹{self.directory_to_watch}')
|
||
|
|
event_handler = Handler()
|
||
|
|
self.observer.schedule(event_handler, self.directory_to_watch, recursive=True)
|
||
|
|
self.observer.start()
|
||
|
|
try:
|
||
|
|
while True:
|
||
|
|
time.sleep(1)
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
self.observer.stop()
|
||
|
|
finally:
|
||
|
|
self.observer.join()
|
||
|
|
|
||
|
|
def join(self):
|
||
|
|
self.observer.join()
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class Handler(FileSystemEventHandler):
|
||
|
|
@staticmethod
|
||
|
|
def on_created(event):
|
||
|
|
if not event.is_directory:
|
||
|
|
if event.src_path.endswith('.pdf') or event.src_path.endswith('.csv') or event.src_path.endswith('.xlsx'):
|
||
|
|
print(f"监控到---{event.src_path}---已被创建")
|
||
|
|
time.sleep(1)
|
||
|
|
try:
|
||
|
|
# 构建multipart/form-data请求体
|
||
|
|
boundary = 'Boundary-0123456789'
|
||
|
|
headers = {
|
||
|
|
'Content-Type': f'multipart/form-data; boundary={boundary}'
|
||
|
|
}
|
||
|
|
# 确定文件的MIME类型
|
||
|
|
mime_type, _ = mimetypes.guess_type(event.src_path)
|
||
|
|
|
||
|
|
# 构建请求体
|
||
|
|
body = []
|
||
|
|
body.append(f'--{boundary}')
|
||
|
|
body.append(
|
||
|
|
f'Content-Disposition: form-data; name="file"; filename="{event.src_path}"')
|
||
|
|
body.append(f'Content-Type: {mime_type}')
|
||
|
|
body.append('')
|
||
|
|
with open(event.src_path, mode='r', newline='', encoding='gb2312', errors='ignore') as file:
|
||
|
|
body.append(file.read())
|
||
|
|
body.append(f'--{boundary}--')
|
||
|
|
body.append('')
|
||
|
|
# 将请求体转换为字节串
|
||
|
|
body_bytes = '\r\n'.join(body).encode('utf-8')
|
||
|
|
|
||
|
|
# 创建 HTTP 连接对象
|
||
|
|
conn = http.client.HTTPConnection(server, server_port)
|
||
|
|
# 发送POST请求
|
||
|
|
conn.request("POST", f"{server_url}?deviceKey=titration&passback=false", body=body_bytes, headers=headers)
|
||
|
|
|
||
|
|
# 获取响应
|
||
|
|
response = conn.getresponse()
|
||
|
|
|
||
|
|
# 读取响应内容
|
||
|
|
response_data = response.read()
|
||
|
|
|
||
|
|
# 检查响应状态码
|
||
|
|
if response.status == 200:
|
||
|
|
print('数据发送成功')
|
||
|
|
print(response_data.decode()) # 打印服务器的响应内容
|
||
|
|
else:
|
||
|
|
print('数据发送失败')
|
||
|
|
print(response.status) # 打印错误状态码
|
||
|
|
print(response.reason) # 打印错误状态码
|
||
|
|
print(response_data.decode()) # 打印错误状态码
|
||
|
|
# break # 找到第一个非空行后终止遍历
|
||
|
|
except Exception as e:
|
||
|
|
print(f"发生异常: {e}")
|
||
|
|
finally:
|
||
|
|
# 关闭连接
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
# logger.info(f"当前用户名: {getuser()}")
|
||
|
|
w = Watcher(r"D:\src\logs")
|
||
|
|
w.run()
|