socket implementation directory path related operations

This knowledge needs to pay attention!
    1. Enter the path to the client, the server will need to add to reach the " / " or it may be wrong
     2 . Locate the directory contents of the call path of listdir os () method returns a list of the form, it must be formatted as string
     3 . directory contents formatted as a string, you need to use a character as a delimiter, then the delimiter .join () method will be converted to a list of specified delimiter string
     4 . reach customers terminal, the output split () list after separated by a specific divider to output contents of a directory
     when the upper directory 5. Back to the server, the principle is to remove the path name of the current path, such as c: / test / 111 to return to the need to remove the test 114
     . 6 ., the current path needs to be separated by the separator windows according to the principles fifth point, but this method is divided into a list of the last element of the list will be an extra space, 
      so that the path is removed at the same time also needs to be removed name spaces, i.e. plus split (after) [: -2 ] to (minus the last two elements of the list) 


# Server layer 
Import Socket
 Import OS 
SK = socket.socket () 
sk.bind ( ( '10.70.2.143 " , 8080 )) 
sk.listen () 
Conn, addr = sk.accept ()
 # Get the current path, the subsequent operations need to be operated based on the path 
abs_path with conn.recv = (1024) .decode ( " UTF -8 " )
 # need to add" / "or it may be wrong 
current_path abs_path + = " / " 
# define sendata function, subsequent operations will need to use the function 
DEF SendData (conn, current_path): 
    list_dir = os.listdir (current_path )
     # the forms of directory listing into a string 
    str_dir = ' - ' .join (list_dir) 
    conn.send (str_dir.encode ( ' UTF-. 8 ')) 
SendData (Conn, current_path) 

# Change directory 
the while . 1 :
     # back one level 
    # to receiving the second command is sent, 
    cmd = conn.recv (1024) .decode ( ' UTF-. 8 ' )
     IF cmd = = ' .. ' :
         # determines whether the root up to the top, such as E: / 
        iF len (current_path.split ( ' / ' )) == 2: # because it will add an extra space after the partition, the two elements are determined 
            conn.send ( " has reached the topmost root directory, you can not go back! " .encode ( ' UTF-8 ' ))
         #Get current path (current_path) into a list and removing the current path name spaces and 
        the else : 
            current_path = current_path.split ( ' / ' ) [: - 2] # because it will add an extra space after separated, it is necessary to remove the current path name and spaces, i.e., a list of the last two elements 
            # paths and then into the operating string, the end of the note need to add '/' or an error occurs 
            current_path = ' / ' .join (current_path) + ' / ' 
            SendData ( Conn, current_path) 
    the else :
     # into the specified folder 
        filename = cmd.split ( '  ' ) [. 1] # because pass over the folder name is located on the second element, the first element is a command name 
        current_path + + = filename ' / '   # To add a folder name to the current path, consisting of a completely new path 
        IF os.path.isdir (current_path): # If you enter a folder 
            SendData (conn, current_path)
         the else : 
            conn.send ( " not a file folder " .encode ( ' UTF-. 8 ' )) 

conn.Close () 
sk.close () 





# Client layer 
Import Socket 
SK = socket.socket () 
sk.connect (( " 10.70.2.143 " , 8080 ))
 # display current path 
the dir_path = iNPUT ( " Please enter the root directory " ) 
sk.send (dir_path.encode (' UTF-. 8 ' )) 
current_dir = sk.recv (1024) .decode ( " UTF-. 8 " )
 Print (current_dir.split ( ' - ' )) 

# Change directory 
the while . 1 : 
    cmd = INPUT ( " Please input operation command " )
     # back one level 
    IF cmd == " .. " : 
        sk.send (cmd.encode ( ' UTF-. 8 ' )) 
        current_dir = sk.recv (1024) .decode ( " UTF-. 8 " )
        Print (current_dir.split ( ' - ' ))
     # into the specified folder 
    IF cmd == " cd " : 
        filename = the INPUT ( " Please enter a folder name " ) 
        sk.send ((cmd + "  " + filename) .encode ( ' UTF-. 8 ' )) 
        current_dir = sk.recv (1024) .decode ( " UTF-. 8 " )
         Print (current_dir.split ( ' - ' )) 

sk.close ()

 

Guess you like

Origin www.cnblogs.com/god-for-speed/p/11719053.html