Server-side:
Process:
1. Create a Servert instance
2. Binding addresses and ports
3. Start monitoring
4. Create a Client Connection instance
5. Waiting for a message from the client
6 .....
1 #the_author = ' liu66 '2 #by python3.x3 #-*-coding = Utf-8-*-4 5 6 ImportSocket,os7 8Server=Socket.socket ()9Server.bind (("localhost", 66))#bind the port to listen onTenServer.listen (5)#Start listening . One #wait for the phone to call in A - whileTrue: - Print("I'm going to start waiting for the phone.") theConn,addr=server.accept ()#Conn As an instance of a connection - Print("the connection and address of the instance are:", CONN,ADDR) - whileTrue: - Print("waiting for data to be received ...") +DATA=CONN.RECV (1024) - if notData: + Print("client has closed ...") A Break at #print (data) - #print (Data.decode ()) - #Conn.send (Data.upper ()) - #Execute cmd command - Try: -res =Os.popen (Data.decode ()). Read () in exceptTypeError: - Print("Please enter the correct cmd") to Else: + Print("------------------") - Print(RES)#print the returned value the Print("------------------") * " "Be sure to judge the length of the return" " $ Print(Len (res))Panax NotoginsengReceived_size="' - ifLen (res) = =0: theres ="cmd is not correct ..." + Print(RES) AConn.send (str (LEN (Res.encode ("Utf-8")) . Encode () ) the +SLEEP_RECV = CONN.RECV (1024) - Print("buffer ...", Sleep_recv.decode ())#Two send commands may appear sticky packets $ $Conn.send (Res.encode ("Utf-8")) - - the - Wuyi the - Print("server is closed ...") WuServer.close ()
Ssh_server
Client:
1. Create a Client instance
2. Connect to the server, address port
3. Sending commands to the server
4 .......
1 #the_author = ' liu66 '2 #by python3.x3 #-*-coding = Utf-8-*-4 5 6 ImportSocket7 8Client=Socket.socket ()9Client.connect (("localhost", 66))Ten One whileTrue: ASend_data=input (">>:"). Strip () - ifLen (send_data) = = 0:Continue -Client.send (Send_data.encode ("Utf-8")) the #Data=client.recv (1024x768) - #print ("Server received data:", Data.decode ()) - - ############ Data Initialization #################### +Data_size=0#the size of the data to accept -Data_res=b"'#The content size of the data to accept +Received_size=0 AReceived_data=b"'#The content of the data to be accepted at ############################################ - " "receive the size of the data you want to accept first" " -DATA_SIZE=CLIENT.RECV (1024) - Print("size of data to accept:", Data_size) - - " "Sending buffered data" " inClient.send ("...". Encode ()) - to whileReceived_size! =Int (data_size): +DATA_RES=CLIENT.RECV (1024) -received_size+=Len (data_res) thereceived_data+=Data_res * Else: $ Print("received data size:", Received_size)Panax Notoginseng Print(Received_data.decode ("Utf-8","Ignore")) - " "decode () the default parameter is strict, which represents an exception thrown when an illegal character is encountered; the if set to ignore, illegal characters are ignored; + if set to replace, it replaces the illegal character with the number. A if set to Xmlcharrefreplace, the character reference of the XML is used. " " the + #Print (Data_res.decode ("Utf-8", "replace")) - $ $ Print("client is closed ...") -Client.close ()
ssh_client
Summary of issues:
- When binding addresses and ports, you need to combine them into one parameter
Client.connect (("localhost", 66))
2. When sending cmd, it is determined that the sent string cannot be empty.
Send_data=input (">>:"). Strip ()ifcontinue
3. The sending data needs to be encoded encode
Client.send (Send_data.encode ("utf-8"))
4. First send the required character length, then receive a buffered message, and then send the content, to prevent two continuous transmission of sticky packets
Conn.send (str (LEN (Res.encode ("utf-8"= conn.recv (1024x768Print (" buffering ... ", Sleep_recv.decode ()) # Two The Send command may appear sticky packets conn.send (Res.encode ( " Utf-8 "))
5. Execute command Exception handling
Try: Res=Os.popen (Data.decode ()). Read ()exceptTypeError:Print("Please enter the correct cmd") Else: Print("------------------") Print(RES)#print the returned value Print("------------------")
6. Send LS command does not report error, return empty, determine whether the return is empty
Print (Len (res)) received_size=" if len (res) = = 0 :"cmd is not correct ... "
7. For data over 1024 processing, determine the length of each accepted bytes, accumulate, until the length equals the total length of the Send
while received_size! = Int (data_size): data_res=client.recv (1024x768) received_size + =len (data_res) received_data+=data_res
8. Print (Xx.decode ()) a solution to the frequent occurrence of illegal character anomalies
Print (Received_data.decode ("utf-8","ignore"))
" " decode () the default parameter is strict, which throws an exception when an illegal character is encountered, or if it is set to ignore, the illegal character is ignored, and if it is set to replace, the illegal character is replaced with a. If set to Xmlcharrefreplace, the character reference of the XML is used. " "
[Python Study Notes] Socket emulates SSH and logs problems encountered