初学python,试着写了一个很简单的备份程序,刚开始使用httplib包,但不知道为何,始终无法发送到服务器上,服务器返回的数据一直是Bad request,所以查了查资料,改用poster包进行发送,最终实现代码如下:
import os,urllib2from poster.encode import multipart_encodefrom poster.streaminghttp import register_openersdbFile = "db.sql"exportCmd = 'mysqldump --user=root --password="1234" oa > '+dbFileos.popen(exportCmd)if os.path.exists(dbFile): try: uploadUrl = "http://www.imaben.com/upload.php" register_openers() datagen,headers = multipart_encode({ "dbfile" : open(dbFile,"rb") }) request = urllib2.Request(uploadUrl, datagen, headers) print urllib2.urlopen(request).read() except Exception,e: print e finally: removeCmd = "rm -rf db.sql" os.popen(removeCmd)else: print "File does not exists!"
本文标题:[原创]通过Python在linux备份数据库并通过HTTP上传到远程服务器本文链接:http://www.maben.com.cn/archives/570.html转载请注明出处 会让你的心态更平和更坦然,也会让你心无旁骛,更会让你的心灵得到解脱和抚慰。