1 #!/usr/bin/python2 #Coding=utf-83 #Server4 fromSocketImport*5 ImportSys,os6 defcommand ():7l=["welcome!\n","command\n","list\n","upload\n","download\n"]8 returnL9 deflist ():TenL=[] One forFileNameinchOs.listdir ('.'): A l.append (filename) - returnL -host="192.168.1.33" theport=5126 -Addr=(Host,port) -sockob=socket (af_inet,sock_stream) - Sockob.bind (ADDR) +Sockob.listen (5) - +A=[] A while1: atSock,addr=sockob.accept () - - while1: -DATA=SOCK.RECV (1024) -Str=data.split (" ") - in ifdata = ="Command": -A=command () to forIinchA: + sock.send (i) - elifdata = ="List": theA=list () * forIinchA: $ sock.send (i)Panax Notoginseng elifstr[0]=="Upload": -F=open (Str[1],'W') theS=SOCK.RECV (1024) + Prints A forIinchS#Range (len (str)): the F.write (i) + Print "upload is ok!" - elifstr[0]=="Download": $F=open (Str[1],'R') $s=F.readlines () - forIinchS: - sock.send (i) the Print "download is ok!" - sockob.close ()WuyiSock.close ()
1 #!/usr/bin/python2 #Coding=utf-83 #Client4 fromSocketImport*5 ImportSYS6 7host='192.168.1.33'8port=51269Addr=(Host,port)Tensockob=socket (af_inet,sock_stream) One Sockob.connect (ADDR) A - while1: -Data=raw_input (">",) theStr=data.split (' ') - if notStr: - Break - elifstr[0]=="Upload": +F=open (Str[1],'R') - sockob.send (data) +s0=F.readlines () A PrintS0 at forIinchs0: - sockob.send (i) - elifstr[0]=="Download": -F=open (Str[1],'W') - sockob.send (data) -S1=SOCKOB.RECV (1024) in f.write (S1) - elifdata=="Command" ordata=="List": to sockob.send (data) +DATA=SOCKOB.RECV (1024) - PrintData the * $Sockob.close ()
PYTHON-TCP Analog FTP File transfer