SSH Service side
ImportSocket,osserver=socket.socket () Server.bind ('localhost', 6666) ) Server.listen () conn, addr=server.accept () whileTrue:Print('wait for new data! ') cmd= CONN.RECV (1024) ifLen (cmd) = =0: BreakData=Os.popen (Cmd.decode ()). Read () cmd_size= Len (Data.encode ('Utf-8')) Print('Data size detected', Cmd_size)ifLen (data) = =0:data='no such command!! 'conn.send (str (LEN (Data.encode ('Utf-8')) . Encode ()) Order= CONN.RECV (1024) Conn.send (Data.encode ('Utf-8')) Print('Transfer Complete! ') Server.close ()
SSH Client
Importsocketclient=socket.socket () Client.connect ('localhost', 6666)) whileTrue:cmd= Input ('>>>:') ifcmd = =0:Continueclient.send (Cmd.encode ('Utf-8')) Cmd_size= CLIENT.RECV (1024) Print('data size detected by server:', Cmd_size)ifLen (cmd_size)! =0:client.send ('you can send the data.'. Encode (encoding='Utf-8')) Receive_size=0 Receive_cmd= b"' whileReceive_size <Int (Cmd_size.decode ()): Data= CLIENT.RECV (1024) Receive_size+=len (data) Receive_cmd+=DataElse: Print('received data size:', Receive_size)Print('Data Reception Complete! ') Print(Receive_cmd.decode ())
Socket file transfer and checksum
Service side
ImportSocket,os,hashlibserver=socket.socket () Server.bind ('localhost', 6666) ) Server.listen () conn, addr=server.accept () whileTrue:Print('wait for new data! ') cmd= CONN.RECV (1024) ifLen (cmd) = =0: Breakfilename= Cmd.decode (). Split () [1] Print(filename)ifOs.path.isfile (filename):#determine if a file existsf = open (filename,'RB')#Open Filem =hashlib.md5 () file_size= Os.stat (filename). st_size#Query File Size Print(file_size) conn.send (str (file_size). Encode ())#send file size to clientCONN.RECV (1024)#wait and accept the client's confirmation, this step can solve the sticky packet problem forLineinchF:#looping files while readingM.update (line)#to derive the MD5 value of each sentenceConn.send (line)#Send to client Print('file MD5:', M.hexdigest ()) F.close () Conn.recv (1024) Conn.send (M.hexdigest (). Encode ())Print('Transfer Complete! ') Server.close ()
Socket file transfer and checksum
Client
Importsocket,hashlibclient=socket.socket () Client.connect ('localhost', 6666)) whileTrue:cmd= Input ('>>>:'). Strip ()ifLen (cmd) = =0:Continue ifCmd.startswith ('Get'):#the transfer file name starts with GET, such as: Get VarClient.send (Cmd.encode ('Utf-8')) File_size= CLIENT.RECV (1024) Print('file size detected by server:', int (file_size)) Client.send ('you can send the data.'. Encode (encoding='Utf-8')) File_total_size=Int (file_size) receive_size=0 filename= Cmd.split () [1]#split with a space and remove the file namef = open ('New_'+filename,'WB') M=hashlib.md5 () whileReceive_size <File_total_size:data= CLIENT.RECV (1024) m.update (data) receive_size+=len (data) f.write (data)Print(File_total_size, receive_size)Else: New_file_md5=m.hexdigest ()Print('Data Reception Complete! ') F.close () Client.send ('you can send MD5 values.'. Encode ()) Serve_md5= CLIENT.RECV (1024) Print('new file MD5:', NEW_FILE_MD5)Print('original file MD5:', SERVE_MD5) client.close ()
Python 3.x learning Note (SOCKET_SSH and socket_ file transfer)