def post_file_curl(path, key, token):
c = pycurl.Curl()
c.setopt(c.POST, 1)
# if path[0] == "\"":
path = path[1:-1]
if os.path.exists(path):
suffix = os.path.splitext(path)[1]
# A fucking dirty hack - rename file
while True:
number = random.randint(10, 100000)
if not os.path.exists(os.path.split(path)[0] + "/" + str(number) + suffix):
newpath = os.path.split(path)[0] + "/" + str(number) + suffix
break
os.rename(path, newpath)
print("rename" + newpath)
bak_path = newpath
print(path)
fields = [('file', (c.FORM_FILE, newpath.encode('gbk'))),
('token', token),
('key', key),
('x:md5', key)]
c.setopt(c.VERBOSE, 1)
c.setopt(c.URL, "http://upload.qiniu.com/")
c.setopt(c.HTTPPOST, fields)
c.setopt(c.NOPROGRESS, 0)
c.setopt(c.PROGRESSFUNCTION, progress)
c.setopt(pycurl.CONNECTTIMEOUT, 60)
c.setopt(pycurl.TIMEOUT, 600)
try:
info = c.perform()
print(info)
print(fields)
if c.getinfo(c.HTTP_CODE) == 200:
os.rename(newpath, path)
print("rename" + path)
return True
except pycurl.error as e:
print(e)
sys.stdout.write("File no Found!")
return False
if os.path.exists(newpath):
os.rename(newpath, path)
print("rename" + path)
c.close()
return False
评论列表
文章目录