定时发送不同的图片
import requests
import os
from apscheduler.schedulers.blocking import BlockingScheduler
import random
from PIL import Image
def post_url():
url = '要发送的服务器'
random_file = ['1', '2', '3', '4', '5']
file_path = './img/'
file_res = './res/'
for file in sorted(os.listdir(file_path)):
file_random = random.choice(random_file)
file_name = os.path.join(file_path, file)
if len(random_file) == 0:
random_file = ['1', '2', '3', '4', '5']
image = Image.open(file_name)
image.save(file_res + file_random + '.jpg')
filename_res = file_res + file_random + '.jpg'
name = file_random + '.jpg'
dict_file = {'file': open(filename_res, 'rb'),
'Content-Disposition': 'form-data',
'fileName': name
}
random_file.remove(file_random)
with requests.Session() as s:
r = s.post(url, files=dict_file)
print(r.text)
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(post_url, 'interval', seconds=3)
scheduler.start()