我必须开发一个脚本,该脚本每次在特定目录中创建一个文件时,都应在必须返回一些数据的特定API的主体调用中使用此文件。 为了监视目录,我正在使用看门狗python库,该库采用已创建文件的路径并调用指定用于调用api的另一种方法,并将文件路径传递给该方法。 这些是上传文件的api的主要规则: api rules for upload file
这些是我写的脚本: 第一个用于看门狗,第二个用于调用api
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from bnotary_api import call_api
if __name__ == "__main__":
patterns = "*"
ignore_patterns = "./file_to_be_notarized/"
#ignora creazione di directory
ignore_directories = True
case_sensitive = True
my_event_handler = PatternMatchingEventHandler(patterns,ignore_patterns, ignore_directories, case_sensitive)
def on_created(event):
path_file_created = event.src_path
print(path_file_created)
print(type(path_file_created))
#file_created = open(path_file_created, 'rb')
file_created = path_file_created
print(file_created)
print(type(file_created))
call_api(file_created)
path = "./file_to_be_notarized/"
my_event_handler.on_created = on_created
#boolean that allow me to catch all the event that occurs even in the sub directories of my current directory
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()
my_observer.join()
import requests
import json
def call_api(file):
print("Inizio chiamata api bnotary\n")
url = 'https://notaryb-api.bcademy.it/api/v1/upload/file'
apiKey = 'my api key'
headers = {'apiKey' : apiKey, 'Content-Type':'application/x-www-form-urlencoded'}
#hash = {'hash': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hashType' : 'SHA-256'}
#file = file.replace('./','')
file = {file : (file , open(file,'rb'), 'application/x-www-form-urlencoded')}
print(type(file))
#file = {'file' : open(file, 'rb') }
print("File passato a request\n",file)
response = requests.post(url, headers=headers, files=file)
print("Url della richiesta",response.request.url)
print("Header della richiesta:\n", response.request.headers)
print("Body della richiesta: \n",response.request.body)
print("Status code risposta:\n",response.status_code)
print("Response header:\n", response.headers)
#print("Response Body:\n" ,response.body)
print("Json risposta\n", response.json())
这是我收到的回复:
./file_to_be_notarized/65.txt
<class 'str'>
./file_to_be_notarized/65.txt
<class 'str'>
Inizio chiamata api bnotary
<class 'dict'>
File passato a request
{'./file_to_be_notarized/65.txt': ('./file_to_be_notarized/65.txt', <_io.BufferedReader name='./file_to_be_notarized/65.txt'>, 'application/x-www-form-urlencoded')}
Url della richiesta https://notaryb-api.bcademy.it/api/v1/upload/file
Header della richiesta:
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'apiKey': '3581ca332d59abbbb665c6ec993642ab8c576b06e95e7a916e06edd6fd9b49ba', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': '239'}
Body della richiesta:
b'--2d0b428db2846b7c39b01ed9c6a2ba4c\r\nContent-Disposition: form-data; name="./file_to_be_notarized/65.txt"; filename="./file_to_be_notarized/65.txt"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n\r\n--2d0b428db2846b7c39b01ed9c6a2ba4c--\r\n'
Status code risposta:
200
Response header:
{'Date': 'Thu, 19 Dec 2019 17:11:44 GMT', 'Server': 'Apache/2.4.18 (Ubuntu)', 'X-Powered-By': 'Express', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '85', 'ETag': 'W/"55-wYAYyT9/kMsIazT9gYE2c7VFqm8"', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive'}
Json risposta
{'status': 'KO', 'error': "TypeError: Cannot read property 'originalname' of undefined"}
尤其是我不明白为什么会返回此类错误:
{'status': 'KO', 'error': "TypeError: Cannot read property 'originalname' of undefined"}