Python 3.x Study Notes 13 (socket_ssh and socket _ file transfer), pythonsocket_ssh

Source: Internet
Author: User
Tags ssh server

Python 3.x Study Notes 13 (socket_ssh and socket _ file transfer), pythonsocket_ssh

Ssh Server

Import socket, osserver = socket. socket () server. bind ('localhost', 6666) server. listen () conn, addr = server. accept () while True: print ('Wait for new data! ') Cmd = conn. recv (1024) if len (cmd) = 0: break data = OS. popen (cmd. decode ()). read () bytes _size = len (data. encode ('utf-8') print ('detected data size', bytes _size) if len (data) = 0: data = 'no such command !! 'Conn. send (str (len (data. encode ('utf-8 '))). encode () order = conn. recv (1, 1024) conn. send (data. encode ('utf-8') print ('transfer completed! ') Server. close ()

Ssh client

Import socketclient = socket. socket () client. connect ('localhost', 6666) while True: cmd = input ('>>>:') if cmd = 0: continue client. send (cmd. encode ('utf-8') pai_size = client. recv (1024) print ('data size detected by the server: ', 1__size) if len (1__size )! = 0: client. send ('data can be sent '. encode (encoding = 'utf-8') receive_size = 0 receive_cmd = B ''while receive_size <int (inclu_size.decode (): data = client. recv (1024) receive_size + = len (data) receive_cmd + = data else: print ('size of received data: ', receive_size) print ('data reception completed! ') Print (receive_pai.decode ())

 

Socket file transmission and Verification

Server

Import socket, OS, hashlibserver = socket. socket () server. bind ('localhost', 6666) server. listen () conn, addr = server. accept () while True: print ('Wait for new data! ') Cmd = conn. recv (1024) if len (cmd) = 0: break filename = cmd. decode (). split () [1] print (filename) if OS. path. isfile (filename): # determine whether the file exists f = open (filename, 'rb') # open the file m = hashlib. md5 () file_size = OS. stat (filename ). st_size # Query file size print (file_size) conn. send (str (file_size ). encode () # send the file size conn to the client. recv (1024) # Wait for and accept confirmation from the client. This step can solve the problem of sticking to the package. for line in f: # send the file repeatedly while reading. update (line) # obtain the md5 value conn for each sentence. send ( Line) # Send the print ('file md5: ', m. hexdigest () f. close () conn. recv (1, 1024) conn. send (m. hexdigest (). encode () print ('transfer completed! ') Server. close ()

Socket file transmission and Verification

Client

Import socket, hashlibclient = socket. socket () client. connect ('localhost', 6666) while True: cmd = input ('>>> :'). strip () if len (cmd) = 0: continue if cmd. startswith ('get'): # The transfer file name must start with get, for example, get var client. send (cmd. encode ('utf-8') file_size = client. recv (1024) print ('server detected file size: ', int (file_size) client. send ('data can be sent '. encode (encoding = 'utf-8') file_total_size = int (file_size) receive_size = 0 filename = Cmd. split () [1] # split by space and then retrieve the file name f = open ('new _ '+ filename, 'wb') m = hashlib. md5 () while receive_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 completed! ') F. close () client. send ('md5 value can be sent '. encode () serve_md5 = client. recv (1024) print ('new file md5: ', new_file_md5) print ('original file md5:', serve_md5) client. close ()

 

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.