FTP Server
1) Read file name
2) Check if the file exists
3) Open File
4) Check the file size
5) Send file size to client
6) such as client confirmation
7) Start reading and sending data on the side
Download File
Client#-*-coding:utf-8-*-__author__='Shisanjun'ImportSocketImportJSONImportOshost=("127.0.0.1", 9000) Client=Socket.socket () client.connect (host) whileTrue:cmd_str=input ("Please enter a command >>"). Strip ()ifLen (CMD_STR) ==0:Continuecmd_list=Cmd_str.split ()ifcmd_list[0]=="put": ifLen (cmd_list) ==1: Print("Not find put filename") Else: FileName=cmd_list[1] ifos.path.isfile (filename): File_obj=open (filename,'RB') filename2=filename.split ("/") [-1] Print(filename2) Data_stat="%s%s"%(filename2,os.path.getsize (filename)) data={ "filename": filename2,"filesize": os.path.getsize (FileName)} client.send (Json.dumps (data). Encode ("Utf-8")) forLineinchFile_obj:client.send (line) file_obj.close ()Else: Print("can not find file") elifcmd_list[0]=="Get": PassServer-Side#-*-coding:utf-8-*-__author__='Shisanjun'ImportSocketImportJsonhost=("127.0.0.1", 9000) Server=Socket.socket () server.bind (host) Server.listen (5) whiletrue:conn,addr=server.accept () data=CONN.RECV (4096) Print(Data.decode ("Utf-8")) Data_dict=json.loads (Data.decode ("Utf-8")) FileName=data_dict.get ("filename") F=open (filename,"WB") Recesize=0 whileRecesize<data_dict.get ("filesize"): Recefile=CONN.RECV (4096) F.write (recefile) recesize+=Len (recefile) f.close ()
Python Basic Learning log day8-socket uploading files