Python 3.x learning Note (SOCKET_SSH and socket_ file transfer)

Source: Internet
Author: User

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)

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.