Use a Python socket to write an FTP-like script (only the download, upload and download almost no more write)

Source: Internet
Author: User

The introduction of the socket is not much to say, the internet can be searched, directly say the script, to achieve the function of FTP server and client,

Server-side:

Import socketserverimport commandsimport timeclass mysockserver ( Socketserver.baserequesthandler):     def handle (self):         print  "Got a new conn from", self.client_address         while True:             CMD = SELF.REQUEST.RECV (1024x768)              if not cmd:break             cmd = cmd.split ()              if cmd[0] == "Get":                 print  "Start transfer ..."                  with&nbsP;open (cmd[1], ' RB ')  as f:                     self.request.send (F.read ())                                   time.sleep (0.2)                  self.request.send ("OOK")     #这算是个标示吧. Client received this description has been passed                  continue            cmd_result =  Commands.getstatusoutput (cmd[0])    #执行客户端发地来的shell命令              self.request.send (cmd_result[1])              if __name__ ==  ' __main__ ':     host= '     port = 52167      s = socketserver.threadingtcpserver ((host,port),  MySockServer)      s.serve_forever ()      client: Import socketimport time host  =  ' x.x.x.x '   #服务器的地址port  = 52167addr = host,ports = socket.socket (Socket.af_inet,socket. SOCK_STREAM) s.connect (addr) while true:    input = raw_input (' Please  Your input: '). Strip ()     if len (input) ==0:continue    s.send (Input)     input = input.split ()     if Input[0]==  ' Get ':        print  ' get file           with open (input[1], ' WB ')  as f:     &Nbsp;      f.write (S.RECV (1024x768))          Time.sleep (0.2)         if s.recv (1024x768)  ==  "OOK":             print  "down finsh&nbsp ..."              continue       DATA = S.RECV (1024x768)     print data

I've been learning Python all the time, but it's always intermittent. Can't insist. Learn for a long time to write a simple socket, this IQ let people catch nasty ah.

This article is from the Linux management blog, so be sure to keep this source http://hans925.blog.51cto.com/542170/1567973

Use a Python socket to write an FTP-like script (only the download, upload and download almost no more write)

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.