我使用以下函数将mp4文件上传到ftp
def run(self):
while True:
status = "Failed"
#logger.debug("Queue size: %s" %(str(self.queue.qsize())))
(env, pfile) = self.queue.get()
logger.debug("Thread %s Received %s and %s" %(str(self.threadID), pfile, env))
metaName = "%s/%s.evs.xml" %(self.config[env]["upload_folder"], pfile)
mediaName = "%s/%s.mp4" %(self.config[env]["upload_folder"], pfile)
mediaName2 = "%s.mp4" %(pfile,)
logger.info("Thread %s - Uploading metadata to %s FTP: %s" %(str(self.threadID), env, metaName))
try:
ur = uploadFTPMP(metaName, env)
status = "Success"
except:
logger.debug("Thread %s - Uploading of metadata %s to %s failed" %(str(self.threadID), metaName, env))
uploadComplete(pfile, status, env)
if status == "Success":
logger.info("Thread %s - Sleeping to allow cms to pick up xml" %(str(self.threadID)))
time.sleep(90)
logger.info( "Thread %s - Uploading mediafile %s to %s" %(str(self.threadID), mediaName, env))
ur = uploadFTP(mediaName, env)
status = "Success"
filename = os.path.basename(mediaName)
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig(level=logging.INFO)
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.INFO)
requests_log.propagate = True
url = 'http://www.webdev.com/web/home/testPOST'
headers = {'content-type': 'application/json'}
payload = {'mediaName': mediaName2 ,'status': 'sucsses'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
r.text
r.status_code
r.connection.close()
uploadComplete(pfile, status, env)
logger.info("Thread %s - Upload completed with status %s" %(str(self.threadID), status))
self.queue.task_done()
我正在尝试在上传之前修改mp4文件,时间戳到文件末尾,因此文件名将为video-%H%M%S.mp4
,然后将上传。
然后,当我执行post请求时,如你所见,它现在发送2个参数,我需要添加第3个参数,这是带有时间戳的新名称。任何实现这种逻辑的技巧?
这也是我的ftp功能。
def uploadFTPMP(filepath, env):
global config
ftpsrv = config[env]["active"]
ftpusr = config[env]["ftpuser"]
ftppwd = config[env]["ftppass"]
filename = os.path.basename(filepath)
try:
ftp = ftplib.FTP(ftpsrv)
ftp.login(ftpusr, ftppwd)
except:
logger.error("Ftp connection error has occurred")
raise
else:
f = open(filepath, "r")
cmd = "STOR %s" %(filename)
out = ftp.storbinary(cmd, f)
f.close()
ftp.quit()
return out
答案 0 :(得分:3)
您应该将日期时间传递给文件名。
import datetime
medianame = 'somefile_{timestamp}.mp4'.format(
timestamp=datetime.datetime.now().isoformat()
)
日期时间也可以根据需要进行格式化,例如datetime.datetime.now().strftime('%Y-%m-%d_%H%M')
。 strftime()
documentation 有一个可以使用的已接受格式化指令列表。