Python for simple file transfer with MySQL backup script sharing

Source: Internet
Author: User
Implement simple Server/client file transfer with Python:

Server-side:

#!/usr/bin/pythonimport Socketserver, Timeclass MyServer (socketserver.baserequesthandler): UserInfo = {' Leonis ': ' Leonis ', ' hudeyong ': ' Hudeyong ', ' Mudan ': ' Mudan '}def handle (self):p rint ' Connected from ', Self.client_addresswhile True:receiveddata = SELF.REQUEST.RECV (8192) if not receiveddata:continueelif receiveddata = = ' Hi, server ': Self.request.sendall (' Hi, client ') elif receiveddata.startswith (' name '): Self.clientname = Receiveddata.split (': ') [- 1]if MyServer.userInfo.has_key (self.clientname): Self.request.sendall (' valid ') Else:self.request.sendall (' Invalid ') elif receiveddata.startswith (' pwd '): self.clientpwd = Receiveddata.split (': ') [ -1]if self.clientpwd = = Myserver.userinfo[self.clientname]:self.request.sendall (' valid ') Time.sleep (5) sfile = open (' down.sh ', ' RB ') while True:data = Sfile.read (1024x768) if not data:breakwhile len (data) > 0:intsent = self.request.send (data) data = Data[intsent: ]time.sleep (3) self.request.sendall (' EOF ') else:self.request.sendall (' invalid ') elif Receiveddata = = ' Bye ': Breakself.request.close () print ' Disconnected from ', self.client_addressprintif __name__ = = ' __main__ ':p rint ' Server is started\nwaiting for connection...\n ' SRV = Socketserver.threadingtcpserver ((' IP ', 50000), MyServer) srv.serve_forever ()

Client:

Import socket, Timeclass myclient:def __init__ (self):p rint ' Prepare for connecting ... ' Def connect (self): sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM) sock.connect (' IP ', 50000) sock.sendall (' Hi, server ') Self.response = Sock.recv (8192) print ' Server: ', Self.responseself.s = Raw_input ("Server:do you want get the ' thinking in Python ' file? ( y/n): "If self.s = = ' Y ': while True:self.name = Raw_input (' Server:input Our Name: ') sock.sendall (' name: ' + Self.name.strip ()) Self.response = Sock.recv (8192) if self.response = = ' valid ': Breakelse:print ' server:invalid username ' while True: Self.pwd = Raw_input (' Server:input our password: ') sock.sendall (' pwd: ' + self.pwd.strip ()) Self.response = Sock.recv ( 8192) If Self.response = = ' valid ':p rint ' please wait ... ' F = open (' down.sh ', ' WB ') while True:data = Sock.recv (1024x768) if data = = ' EOF ': breakf.write (data) F.flush () f.close () print ' download finished ' breakelse:print ' server:invalid password ' sock. Sendall (' Bye ') sock.close () print ' disconnected ' if __name__ = = ' __mAin__ ': client = MyClient () client.connect () 

Because of the fear of server data security, so wrote this script, combined with the above shared server/client file transfer, you can back up the site data to local, safe and reliable

#!/usr/bin/python# Filename:webbak.pyimport osimport timeimport tarfile os.chdir ('/home/web/') #切换目录source = ' Leonis ' Bakdir = '/home/web/leonis/' # mysql dumpdump = ' mysqldump ' dbuser = ' XXXXXXX ' dbpwd = ' xxxxxxxxxxx ' dbname = ' XXXXXXXX ' Sqlfile = '/home/web/leonis/leonis.sql ' sql = '%s-u%s-p%s%s >%s '% (dump,dbuser,dbpwd,dbname,sqlfile) if Os.path.exi STS (sqlfile):   os.remove (sqlfile) Else:  print ' then'll dump SQL file ' result = Os.popen (SQL) If result:       # Print ("SQL backup completed!") Else:print ("SQL backup failed!") # gzip compression is named with day date filename = bakdir + time.strftime ('%y%m%d ') + '. tar.gz ' tar = tarfile.open (filename, "W:gz") Tar.add (source ) Tar.close ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.