Or that topic (the topic and process see Java version), feel the light with Java to write a little new, just happen to learn python, why not take 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.client_addre SS While True:receiveddata = SELF.REQUEST.RECV (8192) if not receiveddata:continue elif receiveddata = = ' Hi, server ': Self.request.sendall (' Hi, client ') elif Receiveddat A.startswith (' name '): Self.clientname = Receiveddata.split (': ') [-1] if MyServer.userInfo.has_key (self.cl Ientname): Self.request.sendall (' valid ') Else:self.request.sendall (' invalid ') Elif receiveddata.startswith (' pwd '): self.clientpwd = Receiveddata.split (': ') [-1] if SELF.CLIENTP WD = = Myserver.userinfo[self.clientname]: Self.request.sendall (' valid ') Time.sleep (5) SF ile = open (' Pynet.pdf ',' RB ') while True:data = Sfile.read (1024x768) 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 ' Server 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 in a class in Java, which is the entry of a class.
Python in the Socketserver module provides a lot of practical ready-made classes, Baserequesthandler is a, its role is to fork a thread for each request, as long as inherit it, there is this ability, haha, really good.
Of course, we inherited the Baserequesthandler, which is to override its handle method, just as Java inherits the thread and implements the Run method. In fact, the content of this handle method is exactly the same as our Java version of the run function.
Line-30 to line-43 is the main content of the file download process. Look very familiar to the AH:)
Here after the file sent a "EOF", tell the client file is finished.
Client:
Import socket, Time class Myclient:def __init__ (self): print ' Prepare for connecting ... ' Def connect (self): Sock = Socket.socket (socket.af_inet, socket. Sock_stream) sock.connect ((' localhost ', 50000)) Sock.sendall (' Hi, server ') Self.response = Sock.recv (8192) print ' Server: ', self.response self.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 ': Break Else:print ' server:invalid username ' while True:self.pwd = Raw_input (' Se Rver: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 (1024x768) 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 = MyClient () client.connect ()
line-34 to line-41 processing the file download, the client receives the server's "EOF" signal and knows that the file has been passed.
Finally need to explain the Python file, because it is a built-in type, so do not want Java as there are so many reader,writer,input,ouput ah. In Python, when you open or create a file, it is mainly distinguished by the pattern (mode).
Python's network programming is really simple, because it provides a variety of functions of the already written class, direct inheritance is OK.
Python is still learning, the above example running is no problem, but certainly not good enough to write, but also to learn