Simple File Transfer server and client _python implemented by Python

Source: Internet
Author: User
Tags sleep

or the topic (see the Java version of the topic and process), feel the light in Java to write a little new and no, just learned python, why not take it for a use, hehe:
Server-side:

Import Socketserver, Time Class MyServer (Socketserver.baserequesthandler): UserInfo = {' yangsq ': ' YANGSQ ' , ' Hudeyong ': ' Hudeyong ', ' Mudan ': ' Mudan '} def handle (self): print ' Connected from ', self.cl  
        ient_address while true:receiveddata = SELF.REQUEST.RECV (8192) if not receiveddata:  
          
      Continue elif Receiveddata = = ' Hi, server ': Self.request.sendall (' Hi, client ') Elif receiveddata.startswith (' name '): Self.clientname = Receiveddata.split (': ') [-1] if myserver.us Erinfo.has_key (self.clientname): Self.request.sendall (' valid ') Else:self.request.sendal L (' 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 (' pynet.pdf ', ' RB ') while True:data = Sfile.read (1024) if not  
              Data:break while 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 ': Break self.request. Close () print ' Disconnected from ', self.client_address print if __name__ = = ' __main__ ': print ' S  
  Erver is started\nwaiting for connection...\n ' SRV = socketserver.threadingtcpserver ((' localhost ', 50000), MyServer)        Srv.serve_forever ()


Description:
line-55 to line-58 is equivalent to the main function inside a class in Java, which is the entry of a class.
Python socketserver module provides a lot of useful ready-made classes, baserequesthandler is a, it is a function for each request fork a thread, as long as inherit it, have this ability, haha, really good.
Of course, we inherited the Baserequesthandler, which is override its handle method, just as the Run method is implemented after the thread is inherited in Java. In fact, the content of this handle method is exactly the same as that of our Java version of the run function.
Line-30 to line-43 is the main content for processing file downloads. Looks familiar to me. The
here sends an "EOF" after the file is sent, telling the client that the file is finished.
Client:

Import socket, Time class Myclient:def __init__ (self): print ' Prepare for connecting ... ' Def Connec T (self): sock = Socket.socket (socket.af_inet, socket. Sock_stream) sock.connect ((' localhost ', 50000)) Sock.sendall (' Hi, server ') Self.response = SOCK.RECV (8 print ' Server: ', self.response self.s = Raw_input ("Server:do you want get the ' thinking in Python ' fil E?  
        (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 ': Break Else: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 ': print ' Please wait ... ' 
 
          f = open (' B.pdf ', ' WB ') while True:data = SOCK.RECV (1024) if data =  
 
          = ' EOF ': Break f.write (data) F.flush () F.close ()  
          
 
    print ' Download finished ' break else:print ' server:invalid password ' Sock.sendall (' Bye ') sock.close () print ' disconnected ' if __name__ = ' __main__ ': client = my  
 Client () Client.connect ()

line-34 to line-41 processing file download, the client received the server "EOF" signal, know the file passed.
Finally need to explain the Python file, because it is a built-in type, so do not want to Java that there are so many reader,writer,input,ouput ah. In Python, when you open or create a file, the main difference is in mode.
Python's network programming is really simple, because it provides a variety of features have been written classes, direct inheritance is OK.
Python is still learning, the example above running is no problem, but it is not good enough to write, you have to learn AH

Related Article

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.